如何将新的@types 包用于 TypeScript 类型?
How to use the new @types packages for TypeScript typings?
我已经开始迁移现有的大型 namespace
和 <reference>
基于 TypeScript 的应用程序,以使用 modules
和 @types
NPM 存储库。但是,我 运行 每一秒都会遇到问题。
- 很多图书馆甚至都没有打字。
- 很多图书馆那里的打字已经完全过时了。
- 很多库都有
global
类型文件,结果我不能 import
因为 TS 抱怨它不是模块。
- 当一个库有一个
module
形状的打字文件正确地包含在它的 NPM 包中时(比如 moment.js
),TS 似乎仍然无法找到它,并且错误行 import * as moment from 'moment'
说找不到模块。
这项技术的现状如何?乍一看,它似乎还远未准备好生产。
克服这些问题的技巧和技巧是什么?
我的目标是 es5
并使用 es6
作为 modules
配置。这可能是问题所在吗?据我所知,TS 也支持 es5
输出的 es6
模块语法。
当前的工作方式绝对是生产就绪的;然而,有些事情并不明显,这不是你的错。
让我试着一一回答你的问题。
- Lots of libraries don't even have typings there.
在 TypeScript 2.1 中,只要您在 node_modules
中安装了一个包并且您没有打开 noImplicitAny
,您可以随意导入任何内容。
如果您确实想使用 noImplicitAny
(我会推荐您希望随着时间的推移增长的任何项目),您总是可以在项目源文件夹中创建一个 declarations.d.ts
根据需要声明模块:
// do anything you want when you import `"foo"` now.
declare module "foo";
// can only access the `hello` export when importing `"bar"`.
declare module "bar" {
export var hello;
}
- Lots of libraries have completely outdated typings there.
你绝对应该随意向 DefinitelyTyped 发送拉取请求,但如果你真的时间紧迫,你可以使用我为第 (1) 点提供的方法。
- Lots of libraries have a
global
typings file, resulting that I cannot import
it because TS complains that it's not a module.
如果您需要使用来自 DefinitelyTyped 的全局声明文件,并且它没有自动包含在您的项目中,您可能必须将其添加到 tsconfig.json
的 types
字段中。
请参阅我的其他答案 ,但关键是如果您需要包含 foo
和 bar
包的全局声明,并且您有 @types/foo
和 @types/bar
安装好了,你可以在你的tsconfig.json
.
中写上下面的内容
{
"compilerOptions": {
"types": ["foo", "bar"]
}
}
- When a library has a
module
shaped typing file correctly included in it's NPM package (like moment.js
), TS still seems to be unable to find it, and errors the line import * as moment from 'moment'
saying module is not found.
我认为这与您以 ES6 为目标这一事实有关。尝试在 tsconfig.json
.
中将 "moduleResolution"
选项更改为 "node"
{
"compilerOptions": {
"moduleResolution": "node"
}
}
抱歉,这是一个陷阱。
理由是唯一真正具有这种解析语义的模块加载系统是 CommonJS(Node 使用的策略)。
我同意这可以改进。
我已经开始迁移现有的大型 namespace
和 <reference>
基于 TypeScript 的应用程序,以使用 modules
和 @types
NPM 存储库。但是,我 运行 每一秒都会遇到问题。
- 很多图书馆甚至都没有打字。
- 很多图书馆那里的打字已经完全过时了。
- 很多库都有
global
类型文件,结果我不能import
因为 TS 抱怨它不是模块。 - 当一个库有一个
module
形状的打字文件正确地包含在它的 NPM 包中时(比如moment.js
),TS 似乎仍然无法找到它,并且错误行import * as moment from 'moment'
说找不到模块。
这项技术的现状如何?乍一看,它似乎还远未准备好生产。
克服这些问题的技巧和技巧是什么?
我的目标是 es5
并使用 es6
作为 modules
配置。这可能是问题所在吗?据我所知,TS 也支持 es5
输出的 es6
模块语法。
当前的工作方式绝对是生产就绪的;然而,有些事情并不明显,这不是你的错。
让我试着一一回答你的问题。
- Lots of libraries don't even have typings there.
在 TypeScript 2.1 中,只要您在 node_modules
中安装了一个包并且您没有打开 noImplicitAny
,您可以随意导入任何内容。
如果您确实想使用 noImplicitAny
(我会推荐您希望随着时间的推移增长的任何项目),您总是可以在项目源文件夹中创建一个 declarations.d.ts
根据需要声明模块:
// do anything you want when you import `"foo"` now.
declare module "foo";
// can only access the `hello` export when importing `"bar"`.
declare module "bar" {
export var hello;
}
- Lots of libraries have completely outdated typings there.
你绝对应该随意向 DefinitelyTyped 发送拉取请求,但如果你真的时间紧迫,你可以使用我为第 (1) 点提供的方法。
- Lots of libraries have a
global
typings file, resulting that I cannotimport
it because TS complains that it's not a module.
如果您需要使用来自 DefinitelyTyped 的全局声明文件,并且它没有自动包含在您的项目中,您可能必须将其添加到 tsconfig.json
的 types
字段中。
请参阅我的其他答案 foo
和 bar
包的全局声明,并且您有 @types/foo
和 @types/bar
安装好了,你可以在你的tsconfig.json
.
{
"compilerOptions": {
"types": ["foo", "bar"]
}
}
- When a library has a
module
shaped typing file correctly included in it's NPM package (likemoment.js
), TS still seems to be unable to find it, and errors the lineimport * as moment from 'moment'
saying module is not found.
我认为这与您以 ES6 为目标这一事实有关。尝试在 tsconfig.json
.
"moduleResolution"
选项更改为 "node"
{
"compilerOptions": {
"moduleResolution": "node"
}
}
抱歉,这是一个陷阱。 理由是唯一真正具有这种解析语义的模块加载系统是 CommonJS(Node 使用的策略)。 我同意这可以改进。