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
How to configure tsconfig.json for typings not in @types?
提问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/lodash
and ./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.js
to the types
in your tsconfig.json
:
你只需要添加zone.js
到types
你的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.json
and all the @types/*
packages would be automatically referenced.
因此,您可以删除您的类型声明,tsconfig.json
并且所有@types/*
包都将被自动引用。
In order to get zone.js
to 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.ts
at 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.json
or as a ///<reference>
. For tsconfig
add the include
field:
您可以将文件放在任何地方,但您需要通过将其添加tsconfig.json
到///<reference>
. 对于tsconfig
添加include
字段:
{
"compileOnSave": false,
"compilerOptions": {
..
},
"include": [
"zone.d.ts",
]
}