简体   繁体   English

打字稿 2.0。 tsconfig.json 中的“类型”字段

[英]Typescript 2.0. “types” field in tsconfig.json

I do not understand the meaning of types field in tsconfig.json .我不明白tsconfig.jsontypes字段的tsconfig.json In documentation I have read such text:在文档中,我读过这样的文字:

"types": {
  "description": "Type declaration files to be included in compilation. Requires TypeScript version 2.0 or later.",
  "type": "array",
  "items": {
    "type": "string"
  }
},

As far, as I understand if I install @types/express I should add such string in tsconfig.json就我所知,如果我安装@types/express我应该在tsconfig.json添加这样的字符串

{
  "compilerOptions": {
     ...
     "types": ["lodash"]
   }
} 

but everything works fine without it.但没有它一切正常。 And now I do not understand, why I need types field现在我不明白,为什么我需要types字段

As of TypeScript 2.* the 'tsconfig.json' has the following two properties available:从 TypeScript 2.* 开始,“tsconfig.json”具有以下两个可用属性:

{
    'typeRoots': [],
    'types': [] 
}

I'll detail both in order.我将按顺序详细说明。


  1. 'typeRoots' specifies root folders in which the transpiler should look for type definitions (eg: 'node_modules'). 'typeRoots' 指定转译器应在其中查找类型定义的根文件夹(例如:'node_modules')。

    • If you've been using typescript, you know that for different libraries that have not been written using typescript, you need definitions in order for the compiler to recognize global variables and to have IntelliSense support.如果您一直在使用 typescript,您就会知道对于未使用 typescript 编写的不同库,您需要定义以便编译器识别全局变量并获得 IntelliSense 支持。

    • This issue has been tackled by projects (repos) such as 'DefinatelyTyped' that use tools such as tsd or typings module to download typings required for your project, but they also come with their own 'json' file that needs to be maintained separately.此问题已通过项目(回购)如“DefinatelyTyped”是使用的工具,如TSD分型模块来为你的项目需要下载分型解决,但他们也带着自己的“JSON”文件,该文件需要单独维护。

    • With TS2.* you can now fetch definition dependencies using 'npm'.使用 TS2.*,您现在可以使用“npm”获取定义依赖项。 So instead of using a seperate cli library like tsd or typings , you can now just use: npm i @types/{LIB} this way, all dependencies are managed using package.json and you can easily eliminate the necessity of another 'json' file to maintain in your project.因此,而不是使用像TSD分型一个单独的CLI库,你可以现在就用: npm i @types/{LIB}这种方式,所有依赖正在使用的package.json管理,你可以很容易消除的必要性另一个“JSON”要在项目中维护的文件。


  1. 'types' are the actual library names that will be found in the typeRoot. 'types' 是将在 typeRoot 中找到的实际库名称。

    • so let's say you have the default configuration for typeRoots which would look something like:所以假设你有 typeRoots 的默认配置,它看起来像:

       "typeRoots": [ "./node_modules/@types" ]
    • let's say you now want to use jasmine as a test framework for your project, so you have your typeRoot folder configured, all you have too do now is execute: npm i @types/jasmine --save-dev假设你现在想使用 jasmine 作为你项目的测试框架,所以你已经配置了typeRoot文件夹,你现在要做的就是执行: npm i @types/jasmine --save-dev

    • after the definition package is installed you just need to configure your 'types' property in 'tsconfig.json' as follows:安装定义包后,您只需要在 'tsconfig.json' 中配置您的 'types' 属性,如下所示:

       "types": [ "core-js", "jasmine", "requirejs", "chance" ]

To conclude, basically you tell the TS compiler the following:总而言之,您基本上告诉 TS 编译器以下内容:

typeRoots : You need to look for typings in these folders. typeRoots :您需要在这些文件夹中查找类型。 types : In one of the folders provided above, you will find definitions for theses framworks (subfolders). types :在上面提供的文件夹之一中,您将找到这些框架(子文件夹)的定义。

So using the scenario above, and if we add another root:所以使用上面的场景,如果我们添加另一个根:

"typeRoots": [
    "./node_modules/@types",
    "./custom_definitions"
],
"types": [
    "jasmine",
]

TS will now look for definition files in TS 现在将在

./node_modules/@types/jasmine

or

./custom_definitions/jasmine

Hope this helps!希望这有帮助!

You don't need the types field necessarily.您不一定需要 types 字段。 Here is the important part to note from the documentation :这是文档中需要注意的重要部分:

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 中的包都被认为是可见的

So, if you have followed convention or used a tooling set such as npm to download @types packages, you don't need to configure typeRoots or types in your configuration as it will work out of the box with the default folder structure.因此,如果您遵循约定或使用诸如 npm 之类的工具集来下载 @types 包,则无需在配置中配置typeRoots类型,因为它将使用默认文件夹结构开箱即用。

Small addition to other answers: @types property in tsconfig.json is used mostly for global declarations (logic which you can use without import ing modules).其他答案的小补充: tsconfig.json 中的 @types 属性主要用于全局声明(无需import模块即可使用的逻辑)。 So, it won't limit your types if you import .因此,如果您import . Eg you have this package: node_modules/@types/foo .例如,你有这个包: node_modules/@types/foo And your @types prop equals to [bar] .而你的@types道具等于[bar] You will discover that this is still working if foo is a module based logic:如果foo是基于模块的逻辑,您会发现这仍然有效:

import {A, B, C} from 'foo'

see TS docs:请参阅 TS 文档:

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