打字稿,窗口类型增强不起作用

时间:2019-11-19 07:28:08

标签: typescript

root/global.d.ts

import {GGMessage} from "./GGMessage";
export {}

declare global {
    export interface Window {onWebsocketsMessageReceived: (data: ArrayBuffer) => GGMessage | null}
}

root/src/webSocketProxy.ts

window.onWebsocketsMessageReceived

问题-上面的行显示:

Error:(30, 36) TS2339: Property 'onWebsocketsMessageReceived' does not exist on type 'Window & typeof globalThis'.

为什么会这样?另外,尽管我无法确定确切的更改,但我确定整个过程在不久前就可以正常进行了。尽管如此,相关的(提供的)内容看起来确实可以正确编写。

tsconfig.json

{
  "include": [
    "src/**/*"
  ],
  "compilerOptions": {
    "target": "es2016",
    "module": "commonjs",
     "sourceMap": true,
     "outDir": "./dist",
    "strict": true,
     "noImplicitAny": false,
    "strictNullChecks": false,
  }
}

1 个答案:

答案 0 :(得分:1)

只需在您的Window中声明root/global.d.ts接口

declare interface Window {
 onWebsocketsMessageReceived: (data: ArrayBuffer) => GGMessage | null
}

它将自动合并到lib.dom.d.ts中的定义中。