我正在尝试为在浏览器上运行的js框架编写一个环境定义,但是我发现该框架会覆盖StorageManager并使它成为静态对象。
这是js代码:
function StorageManager() {
throw new Error('This is a static class');
}
StorageManager.save = function(savefileId, json) {
//omitted
};
StorageManager.load = function(savefileId) {
//omitted
};
这是我从here复制的声明代码,我的工作基于此项目:
/**
* StorageManager
*
* The static class that manages storage for saving game data.
*/
interface StorageManagerStatic {
save(savefileId: number, json: string): void;
load(savefileId: number): string;
//other methods omitted.
}
declare var StorageManager: StorageManagerStatic;
但是由于StorageManager是在lib.dom.d.ts上声明的,因此vscode提示我在Subsequent variable declarations must have the same type. Variable 'StorageManager' must be of type '{ new (): StorageManager; prototype: StorageManager; }', but here has type 'StorageManagerStatic'.
我想将StorageManager的类型重新声明为StorageManagerStatic,有什么方法可以实现?
答案 0 :(得分:0)
根据Titian Cernicova-Dragomir,如果不修改TypeScript内置的lib.dom.d.ts
,我将无法实现。
我不是重新声明内置变量,而是提出了这样的解决方法:
interface Window{
StorageManager: StorageManagerStatic;
}
使用StorageManager时,我需要编写以下代码:
window.StorageManager.load()
非常优雅,但我找不到更好的解决方案。