prefer-global-this
April 8, 2026 ยท View on GitHub
๐ Prefer globalThis over window, self, and global.
๐ผ This rule is enabled in the following configs: โ
recommended, โ๏ธ unopinionated.
๐ง This rule is automatically fixable by the --fix CLI option.
This rule will enforce the use of globalThis over window, self, and global.
However, there are several exceptions that remain permitted:
- Certain window-specific APIs, such as
window.innerHeight - Window-specific events, such as
window.addEventListener('resize') - Computed property access on
window, such aswindow[foo]
The complete list of permitted APIs can be found in the rule's source code.
Examples
// โ
window;
// โ
globalThis;
// โ
window.foo;
// โ
globalThis.foo;
// โ
global;
// โ
globalThis;
// โ
global.foo;
// โ
globalThis.foo;
// โ
const {foo} = window;
// โ
const {foo} = globalThis;
// โ
window.navigator;
// โ
globalThis.navigator;
// โ
window.location;
// โ
globalThis.location;
// โ
window.innerWidth;
// โ
window.innerHeight;
// โ
window.addEventListener('click', () => {});
// โ
globalThis.addEventListener('click', () => {});
// โ
window.addEventListener('resize', () => {});
// โ
window.addEventListener('load', () => {});
// โ
window.addEventListener('unload', () => {});