eslint-plugin-no-typeof-window-undefined
ESLint rule that helps avoid typeof window === "undefined"
check and fix it to typeof document === "undefined"
instead.
It's recommended to use document
over window
because server runtimes like Deno have a global window available.
Installation
You'll first need to install ESLint:
$ pnpm add eslint --D
$ npm i eslint --save-dev
Next, install eslint-plugin-no-typeof-window-undefined
:
$ pnpm add -D eslint-plugin-no-typeof-window-undefined
$ npm install eslint-plugin-no-typeof-window-undefined --save-dev
Note: If you installed ESLint globally (using the -g
flag) then you must also
install eslint-plugin-no-typeof-window-undefined
globally.
Usage
Add no-typeof-window-undefined
to the plugins section of your .eslintrc
configuration file. You can omit
the eslint-plugin-
prefix:
{
"plugins": [
"no-typeof-window-undefined"
]
}
Then configure the rule under the rules section.
{
"rules": {
"no-typeof-window-undefined/no-typeof-window-undefined": "warn"
}
}
Or enable it with defaults
{
"extends": [
"plugin:no-typeof-window-undefined/recommended"
]
}
Often it makes sense to enable no-typeof-window-undefined
only for certain files/directories. For cases like that, use
override key of eslint config:
{
"plugins": ["no-typeof-window-undefined"],
"rules": {
"no-typeof-window-undefined/no-typeof-window-undefined": "off"
},
"overrides": [
{
"files": ["src/**/index.{ts,js}"],
"rules": {
"no-typeof-window-undefined/no-typeof-window-undefined": "error
}
}
]
}
Credits