[英]How to import other types in to a typescript .d.ts module
I have a custom .d.ts
file for an external module that looks like我有一个外部模块的自定义.d.ts
文件,看起来像
declare module 'vertices-bounding-box' {
export default function boundingBox(positions: [number,number,number][]): [number, number]
}
Instead of [number,number,number]
I would like to use a type from another module like:而不是[number,number,number]
我想使用另一个模块中的类型,例如:
import { vec3 } from 'gl-matrix';
declare module 'vertices-bounding-box' {
export default function boundingBox(positions: vec3[]): [number, number]
}
TSC complains when I add the import statement for vec3
saying:当我为vec3
添加导入语句时,TSC 抱怨说:
Invalid module name in augmentation. Module 'vertices-bounding-box' resolves to an untyped
module at '/Users/kevzettler/code/hypeworks/node_modules/vertices-bounding-box/index.js', which cannot be augmented. [2665]
You just showed the most prevalent use case for import types in TypeScript:您刚刚展示了 TypeScript 中导入类型最普遍的用例:
Modules can import types declared in other modules.模块可以导入在其他模块中声明的类型。 But non-module global scripts cannot access types declared in modules.但是非模块全局脚本不能访问模块中声明的类型。 Enter import types.输入导入类型。
In other words, this language feature allows to import types of an external module 'gl-matrix'
from within the global scope of your project .d.ts
file like this:换句话说,此语言功能允许从项目.d.ts
文件的全局范围内导入外部模块'gl-matrix'
类型,如下所示:
// ext-module.d.ts, important: don't use ES import statement here
declare module 'vertices-bounding-box' {
export default function boundingBox(
positions: Array<import("gl-matrix").vec3>): [number, number]
}
Note: A file without ES import
/ export
is a global script and not a module.注意:没有 ES import
/ export
文件是全局脚本而不是模块。
'vertices-bounding-box'
does not come with its own types, so these need to be declared in a global script file as shown above. 'vertices-bounding-box'
没有自己的类型,所以这些需要在一个全局脚本文件中声明,如上所示。 For cases, where you wanted to extend already existing types like from DefinitelyTyped / @types
, you would keep the top-level ES import对于你想扩展已经存在的类型的情况,比如从绝对类型/ @types
,你会保留顶级 ES 导入
import { vec3 } from 'gl-matrix';
// ...
in this file, as module augmentation needs a module .在这个文件中,因为模块扩充需要一个module 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.