WebStorm 类型推断

WebStorm typings inferring

我正在使用 TypeScript 构建一个库。我创建了声明文件并将“typings”选项添加到我的 package.json(我也尝试使用“types”)。 现在我的问题是当我尝试安装包时,WebStorm 不会推断类型(例如与它在 Angular2 中的工作方式相反)。

我的 package.json / 打字好像漏掉了什么,所以:

index.d.ts:

declare module 'my-library' {
  export * from "my-library/index";
}

declare module 'my-library/index' {
  export {
    LibraryLogic
  }
  from "my-library/LibraryLogic";
}

declare module 'my-library/LibraryLogic' {
  export class LibraryLogic {
    constructor(someNumber: number);
  }
}

用法:

import { LibraryLogic } from 'my-library';

let a = new LibraryLogic(3);

注意:一切都编译得很好,只是如果 WebStorm 可以自动完成它们,程序员使用我的库会更容易。

Everything compiles just fine, it's just that it would be easier for programmers to use my library if WebStorm would auto-complete them.

  • 如果您的图书馆在 TSC 下工作正常
  • declare module 'my-library/index' 等仅 WebStorm 需要

那么请用 WebStorm 提出问题。

为什么

Webstorm 在 TypeScript 之上使用自己的封闭源代码智能,而 Whosebug 在这里帮不了你。

它应该可以与 Raw TypeScript(又名 VSCode / alm.tools 等)一起工作。

我使用 WebStorm 2016.3.3 时效果很好。

我的设置:

  1. 在我的项目中 package.json 我添加了 "my-library" 作为依赖项
  2. node_modules/my-library/package.json 我有:"types": "./index.d.ts"

Typescript 足够智能,可以找到您的 typings 文件夹,但 WebStorm 不行。您需要将 @types 文件夹添加到 tsconfig.json.

中的 compilerOptions
  "compilerOptions": {
    "typeRoots": ["node_modules/@types", "./typings"] // <-- Add your typings path here
  },

typeRoots 帮助打字稿找到您的自定义打字文件夹。最后,仔细检查您的 IDE 是否正在使用您的 tsconfig.json 文件。

官方文档:https://www.typescriptlang.org/docs/handbook/tsconfig-json.html