如何导入模块的 Typescript 类型以在我自己的类型定义中使用?



我想为选项对象编写一个类型,它本身包括一个来自导入模块的类型,在本例中为Moment.js:

import moment from 'moment'
export type myOptions = {
// ...
myDate: Moment;
}
export function myFunction(options: myOptions) {
// ...
}

这不起作用,似乎"Moment"类型没有与库"Moment"一起导入:

$ npx tsc
example.ts:5:10 - error TS2304: Cannot find name 'Moment'.
5  myDate: Moment;
~~~~~~

Found 1 error.

有没有一种方法可以通过这种方式在其他库的类型上构建?

啊,好吧,这种类型实际上在默认导出时可用:

import moment from 'moment'
export type myOptions = {
myDate: moment.Moment;
}

不过,我不知道如果不阅读你想使用的每个库的源代码,怎么会发现这种东西。

最新更新