如何为不在@types 中的类型配置 tsconfig.json? [英] How to configure tsconfig.json for typings not in @types?

查看:32
本文介绍了如何为不在@types 中的类型配置 tsconfig.json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

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

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

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

我应该在我的 tsconfig.json 文件中放入什么来包含 zone.js.d.ts?

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

推荐答案

您只需要将 zone.js 添加到 tsconfig.json 中的 types:

You just have to add zone.js to the types in your tsconfig.json:

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

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

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

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

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

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

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

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

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

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.

这篇关于如何为不在@types 中的类型配置 tsconfig.json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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