简体   繁体   English

打字稿编译器如何发现带有类型的npm软件包?

[英]how does the typescript compiler discover npm packages with types?

The typescript compiler can be configured via tsconfig.json . 可以通过tsconfig.json配置打字稿编译器。 This also offers settings for discovering type definition files with the typeRoots key. 这还提供了用于使用typeRoots发现类型定义文件typeRoots

By default: 默认:

By default all visible “@types” packages are included in your compilation. 默认情况下,所有可见的“ @types”包均包含在您的编译中。 Packages in node_modules/@types of any enclosing folder are considered visible; 任何封闭文件夹的node_modules / @ types中的软件包都被视为可见; specifically, that means packages within ./node_modules/@types/, ../node_modules/@types/, ../../node_modules/@types/, and so on. 特别是,这表示在./node_modules/@types/、../node_modules/@types/、../../node_modules/@types/等内的软件包。

If typeRoots is specified, only packages under typeRoots will be included 如果指定了typeRoots,则仅包含typeRoots下的软件包

Many packages offer their type definitions in a separate @types/<package-name> package. 许多软件包在单独的@types/<package-name>软件包中提供其类型定义。 Jquery for example. 以Jquery为例。 At the same time, there are packages that ship with bundled type definitions, Firebase does this. 同时,有些软件包附带捆绑的类型定义, Firebase会这样做

Why can the typescript compiler pick up Firebase definitions without editing the typeRoots setting? 为什么打字稿编译器可以在不编辑typeRoots设置的情况下选择Firebase定义? These definitions are not located in @types and, as far as I understand, should not be picked up by default. 这些定义不在@types中,据我所知,默认情况下不应该使用这些定义。

In the publishing docs in TypeScript you see there are two ways to provide types for your users: https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html 在TypeScript的发布文档中,您可以看到两种为用户提供类型的方法: https : //www.typescriptlang.org/docs/handbook/declaration-files/publishing.html

  1. bundling with your npm package, or 与您的npm软件包捆绑在一起,或者
  2. publishing to the @types organization on npm. 在npm上发布到@types组织。

Bigger libraries like Firebase have their own types, see packages/firebase/package.json#L58 or packages/database/package.json#L68 . 诸如Firebase之类的较大库具有其自己的类型,请参阅packages / firebase / package.json#L58packages / database / package.json#L68

Typescripts reads those referenced files in "typings" of all installed packages that are referenced in your code that is being compiled. 打字稿读取正在编译的代码中引用的所有已安装软件包的"typings"的那些引用文件。

The difference between typeRoots and a regular import 'firebase' is : typeRoots和常规import 'firebase' typeRoots ”之间的区别是:

  1. Once you provide type files in typeRoot those types are always used in the compilation process (automatic inclusion). 在typeRoot中提供类型文件后,这些类型将始终在编译过程中使用(自动包含)。 This is great for libraries the change the global context, like jQuery, node, etc. They provide global functions that could otherwise not easily be picked up. 这对于更改全局上下文(如jQuery,node等)的库非常有用。它们提供了否则不易获取的全局函数。

  2. You import it explicitly by saying import {Foo} from 'bar' . 您可以通过说出import {Foo} from 'bar'明确地导入它。 If bar package has a typing property in its package.json, Typescript will pick it up additionally to those in typeRoots. 如果bar封装具有typing在其物业的package.json,打字稿将它捡起来还对那些在typeRoots。

The documentation says this: 文件说:

Keep in mind that automatic inclusion is only important if you're using files with global declarations (as opposed to files declared as modules). 请记住,仅当您使用带有全局声明的文件(而不是声明为模块的文件)时,自动包含才重要。 If you use an import "foo" statement, for instance, TypeScript may still look through node_modules & node_modules/@types folders to find the foo package. 例如,如果使用import“ foo”语句,TypeScript可能仍会遍历node_modules和node_modules / @ types文件夹来查找foo包。

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

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