ts 中如何拓展声明 实现在window中添加属性

ts 中如果没有添加相应的声明就在 window 对象中添加自定义的属性或者方法编辑器会报红提示

file

这个时候我们可以在 .d.ts 声明文件中拓展 Window 对象的属性.

file

1
2
3
4
5
declare module globalThis {
  export interface Window {
    abc: number;
  }
}

然后就可以正确的提示属性内容了

file