我想向window.history对象添加一个属性声明,但收到TS错误提示
我的代码:
const historyInstance = createHashHistory(); // npm hoistory module
window.history.historyInstance = historyInstance;
// ↑ error occurred in there
我的types.ts
是:
interface IHistory extends History {
historyInstance: any;
}
interface Window {
history: IHistory;
// ↑ (property) Window.history: History
// All declarations of 'history' must have identical modifiers.ts(2687)
// Subsequent property declarations must have the same type. Property // 'history' must be of type 'History', but here has type 'IHistory'.ts(2717)
}
发布于 2019-04-28 20:00:34
试着去适应History
接口,而不是去适应Window
接口。创建一个新的*.d.ts
文件,例如,在您的示例中,可能是history.d.ts
。添加以下内容:
export = global;
declare global {
interface History {
historyInstance: any;
}
}
您应该将新创建的history.d.ts
放到types/
文件夹中,然后您必须修改您的tsconfig.json
:
{
"compilerOptions": {
"typeRoots": ["node_modules/@types", "./types"],
...
}
}
Folderstructur类似于:
types/
|--- history.d.ts
tsconfig.json
https://stackoverflow.com/questions/55886549
复制相似问题