声明全局更新窗口无效

时间:2018-08-30 05:18:37

标签: 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个文件可以正常工作...

1 个答案:

答案 0 :(得分:0)

找到了答案here

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

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