You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Window in a web page serves a dual purpose. it implements the Window interface representing the web page main view, but also acts as an alias to the global namespace. All global variables are accessible on the window object at run-time; this applies to builtin JS declarations like Array, Math, JSON, Intl as well as global DOM declarations like HTMLElement, Event, HTMLCollection, etc...
TypeScript today does not model this behavior. For instance window.Math is not defined, so is all the HTMLElement family of declarations.
Most of the patterns window is used to access global declarations are anti-patterns (see a good article about window usage), the main exception is testing for API existence, e.g.: if (window.MutationObserver) { ... }. There is not really a legal way to do this out-of-the-box in TS today.
Our recommendation has been to manually add a declaration for the global object on Window, e.g.:
declare global {interfaceWindow{MutationObserver?: typeofMutationObserver;}}
This is a. inconvenient and b. not easy to find if you are new to TS. Here is a list of issues we have so far related to this issue:
Window in a web page serves a dual purpose. it implements the
Windowinterface representing the web page main view, but also acts as an alias to the global namespace. All global variables are accessible on thewindowobject at run-time; this applies to builtin JS declarations likeArray,Math,JSON,Intlas well as global DOM declarations likeHTMLElement,Event,HTMLCollection, etc...TypeScript today does not model this behavior. For instance
window.Mathis not defined, so is all theHTMLElementfamily of declarations.Most of the patterns
windowis used to access global declarations are anti-patterns (see a good article about window usage), the main exception is testing for API existence, e.g.:if (window.MutationObserver) { ... }. There is not really a legal way to do this out-of-the-box in TS today.Our recommendation has been to manually add a declaration for the global object on
Window, e.g.:This is a. inconvenient and b. not easy to find if you are new to TS. Here is a list of issues we have so far related to this issue:
window.Blobwindow.createImageBitmapwindow.Mathwindow.URLwindow.MutationObserverwindow.PointerEventwindow.MouseEventwindow.Elementwindow.PerformanceObserverIt is also worth noting that the same problem occurs with
selfin web workers andglobalfor nodePossible options here:
globalas a type #14052), and allowWindowto extend from it.Window.