[英]How do I import third-party module in declaration file(*.d.ts)?
I created the type declaration file( index.d.ts
), and I need to use an object of the third-party package('moment.js' in node_modules
) as type. 我创建的类型声明文件(
index.d.ts
),我需要使用第三方包的对象(在“moment.js” node_modules
)类型。
// index.d.ts
import * as Moment from 'moment';
declare var $: any;
declare var google: any;
interface foo {
a: Moment;
b: string;
...
}
I made a code like above, but it doesn't work. 我做了上面的代码,但是没有用。 How do I import a third-party module in
*.d.ts
file? 如何在
*.d.ts
文件中导入第三方模块?
The .d.ts file that ships with Moment wraps wraps everything it exports into a namespace. Moment包装随附的.d.ts文件将其导出的所有内容包装到命名空间中。 So for this lib, importing like
import * as Moment from 'moment';
因此,对于这个库,
import * as Moment from 'moment';
像import * as Moment from 'moment';
一样import * as Moment from 'moment';
means the Moment
variable is a namespace not the interface for a Moment instance itself. 表示
Moment
变量是一个名称空间,而不是Moment实例本身的接口。 What you want is the interface that lives inside the namespace. 您需要的是位于名称空间内的接口。
You have two options to handle this: 您有两种选择来处理此问题:
import * as moment from 'moment';
interface foo {
a: moment.Moment;
b: string;
}
Or deconstruct the interface during the import: 或在导入过程中解构接口:
import { Moment } from 'moment';
interface foo {
a: Moment;
b: string;
}
Personally, I use the first style for files of mine that use lots exported members from the namespace, but if I only need a few I use the second style. 就我个人而言,我使用的是我的文件的第一种样式,该文件使用了从命名空间导出的大量成员,但是如果我只需要少数文件,则使用第二种样式。
edit... 编辑...
If your tsconfig has esModuleInterop
enabled (which is the newish preferred setting), the import from first example can/should remove the * as
bit. 如果您的tsconfig启用了
esModuleInterop
(这是最新的首选设置),则从第一个示例导入可以/应该将* as
位删除。
import moment from 'moment';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.