Title: Expose global declarations on `window` · Issue #19816 · microsoft/TypeScript · GitHub
Open Graph Title: Expose global declarations on `window` · Issue #19816 · microsoft/TypeScript
X Title: Expose global declarations on `window` · Issue #19816 · microsoft/TypeScript
Description: 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; ...
Open Graph Description: 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 acces...
X Description: 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 acces...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/19816
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Expose global declarations on `window`","articleBody":"[Window](https://developer.mozilla.org/en-US/docs/Web/API/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...\r\n\r\nTypeScript today does not model this behavior. For instance `window.Math` is not defined, so is all the `HTMLElement` family of declarations.\r\n\r\nMost of the patterns `window` is used to access global declarations are anti-patterns (see a [good article about window usage](http://2ality.com/2013/09/window.html)), 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.\r\n\r\nOur recommendation has been to manually add a declaration for the global object on `Window`, e.g.:\r\n\r\n```ts\r\ndeclare global {\r\n interface Window {\r\n MutationObserver?: typeof MutationObserver;\r\n }\r\n}\r\n``` \r\n\r\nThis 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:\r\n* #8866 `window.Blob`\r\n* #14402 `window.createImageBitmap`\r\n* #9325 `window.Math`\r\n* #3753 `window.URL`\r\n* #11305 `window.MutationObserver`\r\n* #18756 `window.PointerEvent`\r\n* #19758 `window.MouseEvent`\r\n* #23886 `window.Element`\r\n* #25651 `window.PerformanceObserver`\r\n\r\nIt is also worth noting that the same problem occurs with [`self` in web workers](https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/self) and [`global` for node](https://nodejs.org/api/globals.html#globals_global)\r\n\r\n\r\nPossible options here:\r\n1. support a global type (tracked by #14052), and allow `Window` to extend from it. \r\n2. generate lib.d.ts with **all** global declarations mirrored on `Window`.\r\n","author":{"url":"https://github.com/mhegazy","@type":"Person","name":"mhegazy"},"datePublished":"2017-11-07T18:52:38Z","url":"https://github.com/microsoft/TypeScript/issues/19816"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:f1fcbd4c-2d93-a8b4-a971-c425941a5405 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E080:17FB7:1FFE73D:2892B57:6B01A6FE |
| html-safe-nonce | efa781cebe5499bc013169e63001096f189657a854cd357cccadfb99dad7e887 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMDgwOjE3RkI3OjFGRkU3M0Q6Mjg5MkI1Nzo2QjAxQTZGRSIsInZpc2l0b3JfaWQiOiI2NDEyMDE5NjI5MzI5MzI3ODcwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | dcfab67132c30a0b1d8909231016b22203dc410d387666204f70b233aaefc757 |
| hovercard-subject-tag | issue:271943087 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/19816/issue_layout |
| twitter:image | https://opengraph.githubassets.com/87e6eb939bc80e8abfa4d83a0550a8139637277cfa703b0a5ee1ffc838f96e42/microsoft/TypeScript/issues/19816 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/87e6eb939bc80e8abfa4d83a0550a8139637277cfa703b0a5ee1ffc838f96e42/microsoft/TypeScript/issues/19816 |
| og:image:alt | 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 acces... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mhegazy |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width