unicorn/no-typeof-undefined Pedantic ​
What it does ​
Disallow typeof
comparisons with undefined
.
Why is this bad? ​
Checking if a value is undefined
by using typeof value === 'undefined'
is needlessly verbose. It's generally better to compare against undefined
directly. The only time typeof
is needed is when a global variable potentially does not exists, in which case, using globalThis.value === undefined
may be better.
Examples ​
Examples of incorrect code for this rule:
javascript
typeof foo === "undefined";
Examples of correct code for this rule:
javascript
foo === undefined;
How to use ​
To enable this rule in the CLI or using the config file, you can use:
bash
oxlint --deny unicorn/no-typeof-undefined
json
{
"rules": {
"unicorn/no-typeof-undefined": "error"
}
}