Javascript “为更新窗口声明全局”不起作用
我有以下层次结构Javascript “为更新窗口声明全局”不起作用,javascript,typescript,Javascript,Typescript,我有以下层次结构 / /definitions window.d.ts /components ... tsconfig.json 我的window.d.ts export {} declare global { interface Window { otherKey: any; } } { "compilerOptions": { "outDir": "./dist", "allowJs": true, "target": "es
/
/definitions
window.d.ts
/components
...
tsconfig.json
我的window.d.ts
export {}
declare global {
interface Window { otherKey: any; }
}
{
"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"
]
}
而我的tsconfig.json
是
export {}
declare global {
interface Window { otherKey: any; }
}
{
"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完成时,我会
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
文件工作正常…找到以下答案:
这只是初始化它;这与类型定义无关。是否在
SomeApp.tsx
中导入window.d.ts
?根据您的网页配置,这可能会有所帮助;我在许多文件中使用了window
,我不想一直重新导入它。我希望它成为道路的一部分。如果我真的导入了它,那么是的,它会工作,