src/content/docs/linter/rules/use-global-this.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> :::caution This rule is part of the [nursery](/linter/#nursery) group. This means that it is experimental and the behavior can change at any time. ::: ## Summary - Rule available since: `v2.3.14` - Diagnostic Category: [`lint/nursery/useGlobalThis`](/reference/diagnostics#diagnostic-category) - This rule doesn't have a fix. - The default severity of this rule is [**warning**](/reference/diagnostics#warning). - Sources: - Same as [`unicorn/prefer-global-this`](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-global-this.md){
"linter": {
"rules": {
"nursery": {
"useGlobalThis": "error"
}
}
}
}
Enforce the use of globalThis over window, self, and global.
globalThis is a standard way to access the global object across platforms such as browsers, Web Workers, Node.js and so on, and using it can make your code portable.
However, there are several exceptions that are allowed:
window.innerHeight and self.postMessagewindow.addEventListener('resize')window.foo;
window.addEventListener('click', () => {});
globalThis.foo;
globalThis.addEventListener('click', () => {});
// window/Web Workers-specific APIs are allowed
window.innerWidth;
self.postMessage({ type: 'ready' });
// window-specific events are allowed
window.addEventListener('resize', () => {});