Typescript:重写先前声明的变量



我正在处理一个Typescript声明文件,库的一个类型叫做"Image",如下所示。

declare class Image {
static fromData(data: Data): Image;
static fromFile(filePath: string): Image;
}

不幸的是,我收到一个错误,说"图像"是一个重复的标识符,我不能使用它。

../../../../usr/local/lib/node_modules/typescript/lib/lib.dom.d.ts:16908:13 - error TS2300: Duplicate identifier 'Image'.
16908 declare var Image: {
~~~~~
index.d.ts:2:15
2 declare class Image {
~~~~~
'Image' was also declared here.

有没有什么方法可以覆盖以前的声明(它甚至不是类型,只是HTMLImageElement的构造函数(并重新调整其用途?感谢

这就是为什么您应该始终更喜欢导出类型而不是环境类型(全局类型,存储在*.d.ts文件中(的原因。

只需创建一个文件,例如types.ts,其中包含以下内容

export class Image {
static fromData(data: Data): Image;
static fromFile(filePath: string): Image;
}

然后像往常一样导入import {Image} from './types'

如果仍然存在冲突,可以执行import {Image as MyImage} from './types'

最新更新