javascript - 声明全局更新窗口不起作用
问题描述
我有以下层次结构
/
/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 完成时,我得到
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
文件工作正常...
解决方案
在这里找到了这个答案:
declare global {
interface Window { otherKey: any; }
}
window.otherKey= window.otherKey|| {};
推荐阅读
- javascript - 谷歌表自定义功能最多两列
- javascript - 如何使用 npm pkg 为 React 应用程序启用解析器插件“流、打字稿”
- spring-boot - Spring Rest API 和身份验证的自定义错误对象
- google-cloud-platform - 错误:gs://bucket/SampleVideo.mp4 无法解析为 CSV
- django - 我可以通过 django-rest-auth 登录和注册返回 JSON Web Token (JWT) 吗?
- java - 从 1.8.x 迁移到 PDFBox 2.0.x
- java - 这是强制类加载器使用一个版本的类而不是另一个版本的有效方法吗?
- mysql - 编写查询以查找至少租用一部电影且属于阿灵顿市的客户的全名
- bash - 如何识别bash中两个日期时间之间的差异?
- ios - 如何手动触发 UIBarButtonItem 点击事件?