admin管理员组文章数量:1278883
I am using Mocha/Chai to unit test and am mocking window
as follows:
global.window = { innerHeight: 1000, innerWidth: 1000 };
Understandably, TSLint is plaining that:
Property 'window' does not exist on type 'Global'
A few questions... is Global
a built in NodeJS/Typescript type? I'm currently silencing the warning with declare var global
at the top of the file... but is this the best way to handle this? I noticed I can also resolve the warning with:
declare global {
namespace NodeJS {
interface Global {
window: any;
}
}
}
Preferably, I'd like to extend the existing Global
type to also accept a window
property. Thanks.
I am using Mocha/Chai to unit test and am mocking window
as follows:
global.window = { innerHeight: 1000, innerWidth: 1000 };
Understandably, TSLint is plaining that:
Property 'window' does not exist on type 'Global'
A few questions... is Global
a built in NodeJS/Typescript type? I'm currently silencing the warning with declare var global
at the top of the file... but is this the best way to handle this? I noticed I can also resolve the warning with:
declare global {
namespace NodeJS {
interface Global {
window: any;
}
}
}
Preferably, I'd like to extend the existing Global
type to also accept a window
property. Thanks.
-
1
You can also reference the
window
object like this in TS:(<any>window)
. Not pretty IMHO but does suppress the warning – A1rPun Commented Aug 29, 2017 at 20:46 -
I don't see anything wrong with the declaration merging you're doing... maybe I'd suggest making it
{window: Window}
instead of{window: any}
, but that's up to you. – jcalz Commented Aug 30, 2017 at 13:58
1 Answer
Reset to default 8is
Global
a built-in NodeJS/Typescript type?
Yes. See @types/node/index.d.ts
; in that file, they declare a NodeJS
namespace, and within that, a Global
interface (just as you've done).
I'm currently silencing the warning with
declare var global
Sounds like you don't have the Node typings installed (those typings include the line declare var global: NodeJS.Global;
so you shouldn't have to make any such declarations yourself). Run:
npm install --save-dev @types/node
or, if you use yarn
:
yarn add -D @types/node
Preferably, I'd like to extend the existing
Global
type to also accept awindow
property.
You're mostly there. Just replace window: any;
with window: Window;
. Note: you will need your tsconfig.json
's lib
section to include dom
to provide the Window
interface.
You may soon find that you also want to augment Global document
and navigator
(again, both of these are defined in the dom
lib, and hence require it):
interface Global {
document: Document;
window: Window;
navigator: Navigator;
}
本文标签: javascript(Typescript) Property 39window39 does not exist on type 39Global39Stack Overflow
版权声明:本文标题:javascript - (Typescript) Property 'window' does not exist on type 'Global' - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741221597a2361075.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论