简体   繁体   English

npm 包中的自定义 TypeScript 声明文件不起作用

[英]Custom TypeScript declaration file in npm package not working

I have created a simple package that contains an index.d.ts at the root.我创建了一个简单的包,在根目录下包含一个 index.d.ts。 Here is the content:以下是内容:

declare var foo: number;

I have then created a separate project which adds that package using npm link.然后我创建了一个单独的项目,它使用 npm 链接添加该包。 In Visual Studio Code, foo is not getting picked up.在 Visual Studio Code 中, foo 没有被选中。 I have also tried adding "types": "./index.d.ts" to the package.json where index.d.ts resides.我还尝试将 "types": "./index.d.ts" 添加到 index.d.ts 所在的 package.json 中。 It is not working.它不工作。 In my playground project, I have the following tsconfig.json:在我的游乐场项目中,我有以下 tsconfig.json:

{
  "compilerOptions": {
    "target": "es2018",
    "module": "esnext",
    "jsx": "preserve",
    "strict": false,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "resolveJsonModule": true
  }
}

Why is the index.d.ts not getting picked up by Visual Studio Code in the project?为什么项目中的 Visual Studio Code 没有获取 index.d.ts?

I don't know, how your library package will be included, so first some common rules:我不知道,您的库包将如何包含在内,首先是一些常见规则:

If you import the package as module, index.d.ts at package root (or with package.json entry) will be picked up automatically via module resolution .如果您import包作为模块importindex.d.ts包根目录(或带有package.json条目)的index.d.ts将通过模块解析自动获取。

If package types are just global type declarations (nothing to import from a module), TS would find it automatically in node_modules/@types of any enclosing folder, see typeRoots configuration.如果包类型只是全局类型声明(无需从模块导入),TS 会自动在任何封闭文件夹的node_modules/@types中找到它,请参阅typeRoots配置。

So, let's say you just have declare var foo: number;因此,假设您刚刚declare var foo: number; in the file (it is then a global declaration file) and types are under node_modules/my-lib , not node_modules/@types/my-lib , then you would need to manually tell TS to include it in the compilation.在文件中(它是一个全局声明文件)并且类型在node_modules/my-lib ,而不是node_modules/@types/my-lib ,那么您需要手动告诉 TS 将其包含在编译中。

Easiest way is probably to add it to the files property of tsconfig.json , which specifies additional files to be included explicitely.最简单的方法可能是将它添加到tsconfig.jsonfiles属性,它指定要明确包含的其他文件。 See the good example section in the docs .请参阅文档中的好示例部分。 An alternative would be to adjust typeRoots , but that is probably overkill here.另一种方法是调整typeRoots ,但这在这里可能typeRoots矫枉过正。

My guess is there is a second error cause: npm link will create a symlink and per default TS uses those paths, the symlinks resolve to (the real paths).我的猜测是还有第二个错误原因: npm link将创建一个符号链接,并且默认情况下 TS 使用这些路径,符号链接解析为(真实路径)。

We can fix that by enabling --preserveSymlinks option ( tsc --preserveSymlinks ) to use location of the symbolic link file instead (see also compiler options ).我们可以通过启用--preserveSymlinks选项( tsc --preserveSymlinks )来使用符号链接文件的位置来解决这个问题(另请参阅编译器选项)。 What docs say to this option:什么文档说这个选项:

In this mode, references to modules and packages (eg imports and /// directives) are all resolved relative to the location of the symbolic link file, rather than relative to the path that the symbolic link resolves to.在这种模式下,对模块和包的引用(例如导入和 /// 指令)都是相对于符号链接文件的位置解析的,而不是相对于符号链接解析到的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM