打字稿 2.0.“类型"tsconfig.json 中的字段 [英] Typescript 2.0. "types" field in tsconfig.json

查看:27
本文介绍了打字稿 2.0.“类型"tsconfig.json 中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白tsconfig.jsontypes 字段的含义.在文档中我读过这样的文字:

I do not understand the meaning of types field in 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"
  }
},

据我所知,如果我安装 @types/express 我应该在 tsconfig.json

As far, as I understand if I install @types/express I should add such string in tsconfig.json

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

但是没有它一切正常.现在我不明白,为什么我需要 types 字段

but everything works fine without it. And now I do not understand, why I need types field

推荐答案

从 TypeScript 2.* 开始,'tsconfig.json' 具有以下两个可用属性:

As of TypeScript 2.* the 'tsconfig.json' has the following two properties available:

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

我会按顺序详细说明.

  1. 'typeRoots' 指定转译器应在其中查找类型定义的根文件夹(例如:'node_modules').

  1. 'typeRoots' specifies root folders in which the transpiler should look for type definitions (eg: 'node_modules').

  • 如果您一直在使用 typescript,您就会知道对于未使用 typescript 编写的不同库,您需要定义以便编译器识别全局变量并获得 IntelliSense 支持.

  • 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.

此问题已通过使用诸如 tsdtypings<等工具的DefinatelyTyped"等项目(存储库)解决/em> 模块用于下载项目所需的类型,但它们也带有自己的json"文件,需要单独维护.

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.

借助 TS2.*,您现在可以使用npm"获取定义依赖项.因此,您现在可以使用:npm i @types/{LIB}这样,所有依赖项都使用 package.json 进行管理,您可以轻松地消除在项目中维护另一个json"文件的必要性.

With TS2.* you can now fetch definition dependencies using '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.

<小时>

  1. 'types' 是将在 typeRoot 中找到的实际库名称.

  1. 'types' are the actual library names that will be found in the typeRoot.

  • 假设你有 typeRoots 的默认配置,它看起来像:

  • so let's say you have the default configuration for typeRoots which would look something like:

"typeRoots": [
    "./node_modules/@types"
]

  • 假设您现在想使用 jasmine 作为您项目的测试框架,因此您已经配置了 typeRoot 文件夹,您现在要做的就是执行:npmi @types/jasmine --save-dev

  • 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

    定义包安装后,您只需要在 'tsconfig.json' 中配置您的 'types' 属性,如下所示:

    after the definition package is installed you just need to configure your 'types' property in 'tsconfig.json' as follows:

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

  • <小时>

    总而言之,基本上你告诉 TS 编译器以下内容:


    To conclude, basically you tell the TS compiler the following:

    typeRoots:您需要在这些文件夹中查找类型.types:在上面提供的文件夹之一中,您可以找到这些框架(子文件夹)的定义.

    typeRoots: You need to look for typings in these folders. types: In one of the folders provided above, you will find definitions for theses framworks (subfolders).

    所以使用上面的场景,如果我们添加另一个根:

    So using the scenario above, and if we add another root:

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

    TS 现在将在

    ./node_modules/@types/jasmine

    ./custom_definitions/jasmine

    希望这会有所帮助!

    这篇关于打字稿 2.0.“类型"tsconfig.json 中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆