typescript如何扩展window,添加自定义属性?

tsconfig.json

{
 "compilerOptions": {
 "target": "ES3",
 "module": "ESNext",
 "strict": true,
 "jsx": "react",
 "allowJs": true,
 "importHelpers": true,
 "moduleResolution": "node",
 "skipLibCheck": true,
 "esModuleInterop": true,
 "allowSyntheticDefaultImports": true,
 "sourceMap": true,
 "experimentalDecorators": true,
 "baseUrl": "./",
 "lib": [
 "esnext",
 "dom",
 "dom.iterable",
 "scripthost"
 ]
 }, 
 "include": [
 "src/*/**"
 ]
}

src/test.ts

const a = window.client.a()

src/type.d.ts

export {}
interface Client{
 a: ()=>string
}
declare global {
 interface Window { client: Client; }
}

image.png

既然发现了Client的定义,为啥又说找不到

阅读 10.4k
1 个回答

或许 .d.ts 没引入?你试着把 declare 直接写进 .ts,如果能行就是没引入

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
Microsoft
子站问答
访问
宣传栏