![](/img/trans.png)
[英]Typescript error: Property '_myApiUrl' does not exist on type 'Window'
[英](Typescript) Property 'window' does not exist on type 'Global'
我正在使用Mocha / Chai进行单元测试,并模拟window
如下:
global.window = { innerHeight: 1000, innerWidth: 1000 };
可以理解,TSLint抱怨:
属性“窗口”在类型“全局”上不存在
几个问题... Global
是内置的NodeJS / Typescript类型吗? 我目前正在使用文件顶部的declare var global
消除警告...但这是处理此问题的最佳方法吗? 我注意到我也可以通过以下方式解决警告:
declare global {
namespace NodeJS {
interface Global {
window: any;
}
}
}
最好,我想扩展现有的Global
类型以也接受window
属性。 谢谢。
Global
是内置的NodeJS / Typescript类型吗?
是。 参见@types/node/index.d.ts
; 在该文件中,它们声明一个NodeJS
命名空间,并在其中声明一个Global
接口(正如您所做的那样)。
我目前正在使用
declare var global
消除警告
听起来好像您没有安装Node类型(这些类型包括declare var global: NodeJS.Global;
因此您不必自己进行任何此类声明)。 跑:
npm install --save-dev @types/node
或者,如果您使用yarn
:
yarn add -D @types/node
最好,我想扩展现有的
Global
类型以也接受window
属性。
您大部分都在那儿。 只需替换window: any;
带window: Window;
。 注意:您将需要tsconfig.json
的lib
部分包含dom
来提供Window
界面。
您可能很快会发现,您还想扩充Global document
和navigator
(同样,这两个都是在dom
lib中定义的,因此需要它):
interface Global {
document: Document;
window: Window;
navigator: Navigator;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.