TypeScript 扩展全局 Window 时报错的解决
使用全局 console.log(window.foo) // ❌ Property ‘foo’ does not exist on type 'Window & typeof globalThis'.ts(2339) 需要将自定义变量扩展到全局 types.d.ts declare global { interface Window { foo: string; } } 此时再使用就正常了, console.log(window.foo) // ✅ 如果在进行类型扩展时报如下错误:
可在类型文件中添加如下内容以指定文件为模板,报错消除。 + export {}; declare global { interface Window { foo: string; } } 相关资源 |
The text was updated successfully, but these errors were encountered: |