javascript - 声明全局更新窗口不起作用

标签 javascript typescript

我有以下层次结构

/
  /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文件工作正常......

最佳答案

找到这个答案 here :

declare global {
    interface Window { otherKey: any; }
}

window.otherKey= window.otherKey|| {};

关于javascript - 声明全局更新窗口不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52089673/

相关文章:

javascript - 如果未选中任何复选框,则将状态设置为 0

javascript - 尝试在我的 Rails 应用程序中截屏页面时 PhantomJS.rb 卡住

javascript - 使用不带html文件的dart:js库

javascript - 相同字符串值的两个实例不相等

javascript - Instanbul JS、Vue JS、Vue CLI、Cypress e2e 测试和 Typescript 的覆盖率报告仅显示几个文件

angular - ionic 4/5 使用 ngx-translate 自动添加到 i18n 中的字幕文件

javascript - setTimeout 非法调用 TypeError : Illegal invocation

javascript - 调用react函数

typescript - 在 typescript 中,什么是数组作为函数名?

typescript - Angular 2 Recaptcha 2回调