我有以下层次结构
/
/definitions
window.d.ts
/components
...
tsconfig.json
和我的window.d.ts
存在
export {}
declare global {
interface Window { otherKey: any; }
}
还有我的tsconfig.json
存在
{
"compilerOptions": {
"outDir": "./dist",
"allowJs": true,
"target": "es5",
"lib": ["es5", "es6"],
"jsx": "react",
"sourceMap": true,
"module": "es6",
"preserveConstEnums": true,
"removeComments": true,
"noImplicitAny": false,
"moduleResolution": "node",
"noUnusedParameters": false,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"baseUrl": ".",
"paths": {
"window": ["definitions/window.d.ts"]
}
},
"include": [
"./App.tsx"
]
}
当我运行 dev-server 时,我得到
[at-loader] Using typescript@2.6.1 from typescript and "tsconfig.json" from /correct/path/tp/tsconfig.json.
但是当 webpack 完成时,我得到 p>
ERROR in [at-loader] ./component/SomeApp.tsx:78:35
TS2339: Property 'otherKey' does not exist on type 'Window'.
我只是使用了window.otherKey
没有任何其他declare var window: any
希望otherKey
就会存在。
为什么这不起作用?
编辑
我注意到这只发生在 .tsx
上文件。 .ts
文件工作正常......
最佳答案
找到这个答案 here :
declare global {
interface Window { otherKey: any; }
}
window.otherKey= window.otherKey|| {};
关于javascript - 声明全局更新窗口不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52089673/