typescript 如何为不在@types 中的类型配置 tsconfig.json?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48704150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:13:11  来源:igfitidea点击:

How to configure tsconfig.json for typings not in @types?

typescripttypescript-typings

提问by Steven Liekens

I'm using a tsconfig.json file to specify which typings I want to use in my app.

我正在使用 tsconfig.json 文件来指定我想在我的应用程序中使用哪些类型。

{
   "compilerOptions": {
       "types" : ["node", "lodash", "express"]
   }
}

This imports typings from ./node_modules/@types/node, ./node_modules/@types/lodashand ./node_modules/@types/expres.

这从./node_modules/@types/node,./node_modules/@types/lodash和导入类型./node_modules/@types/expres

My question is how can I configure typings for self-contained modules?

我的问题是如何为自包含模块配置类型?

My example is the zone.js package which has both the library code and type definitions.

我的例子是 zone.js 包,它同时包含库代码和类型定义。

  • ./node_modules/zone.js/dist/zone.js
  • ./node_modules/zone.js/dist/zone.min.js
  • ./node_modules/zone.js/dist/zone.js.d.ts
  • ./node_modules/zone.js/dist/zone.js
  • ./node_modules/zone.js/dist/zone.min.js
  • ./node_modules/zone.js/dist/zone.js.d.ts

What do I put in my tsconfig.json file to include zone.js.d.ts?

我在 tsconfig.json 文件中要包含zone.js.d.ts什么?

采纳答案by a-ctor

You just have to add zone.jsto the typesin your tsconfig.json:

你只需要添加zone.jstypes你的tsconfig.json

{
   "compilerOptions": {
       "types" : ["node", "lodash", "express", "zone.js"]
   }
}

Note that you do not have to include all types like this. Type definitions from the @types/*packages are automatically included.

请注意,您不必包括所有这样的类型。@types/*包中的类型定义会自动包含在内。

So you could remove the types declaration in your tsconfig.jsonand all the @types/*packages would be automatically referenced.

因此,您可以删除您的类型声明,tsconfig.json并且所有@types/*包都将被自动引用。

In order to get zone.jsto work you can either include it in a single file like this:

为了开始zone.js工作,您可以将它包含在一个文件中,如下所示:

/// <reference types="zone.js" />

Or if you want it available in your whole project you can add a index.d.tsat the root of your project and put int the reference there.

或者,如果您希望它在您的整个项目中可用,您可以在项目index.d.ts的根目录中添加一个并将引用放在那里。

回答by Titian Cernicova-Dragomir

You can place the file anywhere, but you need to tell the compiler about it, either by adding it in tsconfig.jsonor as a ///<reference>. For tsconfigadd the includefield:

您可以将文件放在任何地方,但您需要通过将其添加tsconfig.json///<reference>. 对于tsconfig添加include字段:

{
    "compileOnSave": false,
    "compilerOptions": {
        ..
    },
    "include": [
        "zone.d.ts",
    ]
}