相关疑难解决方法(0)

如何为TypeScript配置自定义全局接口(.d.ts文件)?

在过去的几个小时里,我一直在努力解决这个问题,而我似乎无法在互联网上找到任何可以清楚解释这个,据说是简单概念的东西.

我目前正在研究一个使用Webpack2和TypeScript的ReactJS项目.除了一件事之外,一切都完美无缺 - 我无法找到一种方法将我自己编写的界面移动到单独的文件中,以便它们对整个应用程序可见.

出于原型设计的目的,我最初在使用它们的文件中定义了接口,但最终我开始添加一些在多个类中需要的东西,并且当所有问题都开始时.无论我对我做了什么改变tsconfig.json,无论我把文件放在哪里,我的IDE和Webpack都抱怨无法找到名字(Could not find name 'IMyInterface').

这是我当前的tsconfig.json文件:

{
  "compilerOptions": {
    "baseUrl": "src",
    "outDir": "build/dist",
    "module": "commonjs",
    "target": "es5",
    "lib": [
      "es6",
      "dom"
    ],
    "typeRoots": [
      "./node_modules/@types",
      "./typings"
    ],
    "sourceMap": true,
    "allowJs": true,
    "jsx": "react",
    "moduleResolution": "node",
    "rootDir": "src",
    "forceConsistentCasingInFileNames": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noImplicitAny": false,
    "strictNullChecks": true,
    "suppressImplicitAnyIndexErrors": true,
    "noUnusedLocals": true
  },
  "exclude": [
    "node_modules",
    "build",
    "scripts",
    "acceptance-tests",
    "webpack",
    "jest",
    "src/setupTests.ts"
  ],
  "types": [
    "typePatches"
  ]
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我tsconfig.json位于项目目录的根目录中,所有源都在 …

typescript typescript1.8 typescript-typings typescript2.0

48
推荐指数
2
解决办法
3万
查看次数

TypeScript:如何为已安装的npm包定义自定义类型?

我喜欢在TypeScript中使用rx-node

import RxNode from 'rx-node';
Run Code Online (Sandbox Code Playgroud)

我使用npm 安装了rx-node

$ npm install rx-node --save
Run Code Online (Sandbox Code Playgroud)

我搜索了类型定义,但没有任何结果

$ typings search rx-node
No results found for search
Run Code Online (Sandbox Code Playgroud)

如何为已安装的npm模块rx-node定义自定义类型定义?我应该在哪里存储类型定义文件?如何配置TypeScript(tsconfig.jsontypings.json)?

编辑:感谢Aleksey L.David Bohunek,我实现了定义rx-node.d.ts如下所示

declare module "rx-node" {
  import {Observable} from '@reactivex/rxjs';
  import {ReadLine} from "readline";

  function fromReadLineStream(stream: ReadLine): Observable<string>
}
Run Code Online (Sandbox Code Playgroud)

我安装了@activex/rxjs

npm install --save @reactivex/rxjs
Run Code Online (Sandbox Code Playgroud)

因为我收到了错误

node_modules\@reactivex\rxjs\dist\cjs\Observable.d.ts (10,66): Cannot find name 'Promise'. (2304)
Run Code Online (Sandbox Code Playgroud)

我将tsconfig.json中的目标更改es6.

node.js typescript typescript1.8

32
推荐指数
1
解决办法
1万
查看次数

如何将plotly.js导入TypeScript?

我正在尝试导入plotly.jsTypeScript.使用Plotly.js安装npm.在我使用的TypeScript文件中

import 'plotly.js';
Run Code Online (Sandbox Code Playgroud)

没关系但我在代码上遇到错误Plotly.<member>:

error TS2304: Cannot find name 'Plotly'
Run Code Online (Sandbox Code Playgroud)

当我尝试

import Plotly = require('plotly.js');
Run Code Online (Sandbox Code Playgroud)

我越来越

error TS2307: Cannot find module 'plotly.js'.
Run Code Online (Sandbox Code Playgroud)

typescript plotly

10
推荐指数
4
解决办法
9302
查看次数

如何在没有<reference>的情况下在TypeScript中使用'Typings'导入模块?

我真的试图写有测试量角器茉莉花打字稿.TSD现已弃用,所以我必须使用'Typings'来操作TypeScript定义.所以我安装了它:

npm install typings -g
Run Code Online (Sandbox Code Playgroud)

然后我用它来安装像这样的Jasmine和Chance定义:

typings install jasmine --source dt --save –-global
typings install chance--source dt --save –-global
Run Code Online (Sandbox Code Playgroud)

我还添加了"files"部分并排除了'node_modules':

// tsconfig.json
{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "declaration": false,
    "noImplicitAny": false,
    "outDir": "tmp",
    "types": ["node", "jasmine", "chance"]
  },
  "files": [
    "typings/index.d.ts"
  ],
  "include": [
    "lib",
    "specs"
  ],
  "exclude": [
    "node_modules"
  ]
}
Run Code Online (Sandbox Code Playgroud)

问题是WebStorm和Visual Code studio都找不到所有Jasmine方法和'机会'模块的定义.错误是:

**“TS2304: Cannot find name 'describe'”** 
**“TS2307: Cannot find module 'chance'”**
Run Code Online (Sandbox Code Playgroud)

分别.这是我的spec文件: …

jasmine typescript protractor typescript-typings

5
推荐指数
1
解决办法
7739
查看次数

如何在Typescript中覆盖或扩展库类型定义

如果我在Typescript中导入库类型声明。当存在编译器问题时,如何扩展该库的定义,但是如果不是这样,它将是有效的js代码?例如,validate.js类型绑定与实际实现相比非常不准确。如下图所示。

import * as validate from 'validate.js';

declare namespace validate {
  Promise: any;
  async: any;
}
Run Code Online (Sandbox Code Playgroud)

与猫鼬类似,我无法访问modelSchemas属性,但需要。

import * as mongoose from 'mongoose';
declare namespace mongoose {
  export modelSchemas any[];
}
Run Code Online (Sandbox Code Playgroud)

因此,如果我想向现有类型添加定义只是为了关闭编译器。我怎样才能做到这一点?

typescript

5
推荐指数
1
解决办法
5866
查看次数

“找不到模块的声明文件”,typeRoots 文件夹中的自定义声明文件被忽略

您好,我遇到了 TypeScript 问题。我有以下目录结构:

  • package.json:顶级项目
  • node_modules/:所有已安装的库
  • tsconfig.json
  • build/:生成的文件夹
    • 索引.js
    • 处理程序.js
    • ...
  • server/: a folder with a TypeScript server built on top of the build folder
    • typings/
      • handler/
        • index.d.ts
    • tsconfig.json:
    • index.ts: a TypeScript server

The file server/tsconfig.json contains the lines

    "compilerOptions": {
        "typeRoots": [
            "../node_modules/@types",
            "./typings"
        ],
    ...
    "include": [
        "./*.ts",
        "./typings/**/*.ts"
    ],
Run Code Online (Sandbox Code Playgroud)

I import an un-typed JavaScript function in TypeScript in the file server/index.ts:

import { handler } from '../build/handler.js'
... (code using handler) ...
Run Code Online (Sandbox Code Playgroud)

The file server/typings/handler/index.d.ts contains

declare module …
Run Code Online (Sandbox Code Playgroud)

javascript node.js typescript tsc tsconfig

5
推荐指数
0
解决办法
508
查看次数

TypeScript-无法在其他类型根目录TS7016中的类型覆盖中导入自身

我正在使用FeathersJS和TypeScript。由于它仍在开发中(用于羽毛的TS),我发现我需要重写类型以使其能够适应我的情况(例如,可以禁用或启用分页,并且类型应能处理,但此刻不行) 。

在我的tsconfig.json中,我有以下内容:

"typeRoots": [                            /* List of folders to include type definitions from. */
      "./types",
      "node_modules/@types"
    ],
Run Code Online (Sandbox Code Playgroud)

顺序并不重要,因为我也首先尝试过使用node_modules。

我把文件夹feathersjs__feathers./types只有index.d.ts(删除package.json)从DefinitelyTyped类型的精确副本。

这行引起了问题: import * as self from '@feathersjs/feathers';

TS7016找不到模块'@ feathersjs / feathers'的声明文件。“ C:/ Users / marek / dev / system / api — kopia/node_modules/@feathersjs/feathers/lib/index.js”隐式具有“ any”类型。尝试  npm install @types/feathersjs__feathers 是否存在或添加包含以下内容的新声明(.d.ts)文件 declare module 'feathersjs__feathers';

我试图将添加package.json到我的文件夹中,如下所示:

{
  "name": "@types/feathersjs__feathers",
  "version": "0.0.1",
  "types": "index.d.ts"
}
Run Code Online (Sandbox Code Playgroud)

但这并不能解决该问题。如果我安装了@types/feathersjs__feathersnode_modules,则没有错误,但是无论typeRoots它以什么顺序从该类型中获取类型,而不是从我的替代中获取类型。

有什么解决办法吗?

通常使用./types …

node.js typescript feathersjs

2
推荐指数
1
解决办法
676
查看次数