繁体   English   中英

类型“Window & typeof globalThis”上不存在属性。 即使它是在定义文件中定义的

[英]Property does not exist on type 'Window & typeof globalThis'. Even though it is defined in definition file

这是global.d.ts

interface Window {
  routes: any;
  routeMaker: any;
  store: any;
  searchKey: string;
}

据我了解,任何东西都是global.d.ts本质上与

declare global {
  interface Window {
    routes: any;
    routeMaker: any;
    store: any;
    searchKey: string;
  }
}

坐在根目录下的是tsconfig.json我在其中指出了定义文件。

{
  "compilerOptions": {
    "target": "es6",
    "declaration": false,
    "emitDecoratorMetadata": true,
    "baseUrl": ".",
    "experimentalDecorators": true,
    "lib": ["es6", "dom", "webworker"],
    "module": "es6",
    "moduleResolution": "node",
    "sourceMap": true,
    "jsx": "react",
    "typeRoots": ["node_modules/@types", "types"],
    "types": ["node", "webpack-env"],
    "downlevelIteration": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  },
  "exclude": ["**/*.spec.js", "node_modules", "vendor", "public"],
  "include": [
    "types/global.d.ts",
    "**/*.d.ts",
    "app/client/src",
    "app/*.ts"
  ],
  "compileOnSave": false
}

include部分指向定义文件。

但是当我使用定义文件中定义的任何道具时..

window.routeMaker = {};

Property 'routeMaker' does not exist on type 'Window & typeof globalThis'.

这是什么原因造成的? 这是如何解决的?

您需要添加至少一个导入或一个导出才能使 global.d.ts 成为具有全局 scope 效果的外部模块,因此在您的情况下,您可以执行以下操作:

declare global {
  interface Window {
    routes: any;
    routeMaker: any;
    store: any;
    searchKey: string;
  }
}
export {};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM