Skip to content

Latest commit

 

History

History
59 lines (40 loc) · 1.39 KB

no-typeof-undefined.md

File metadata and controls

59 lines (40 loc) · 1.39 KB

Enforce compare with undefined directly

✅ This rule is enabled in the recommended config.

🔧💡 This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.

Enforce compare value with undefined directly instead of compare typeof value with 'undefined'.

Fail

function foo(bar) {
	if (typeof bar === 'undefined') {}
}
import foo from './foo.js';

if (typeof foo.bar !== 'undefined') {}

Pass

function foo(bar) {
	if (foo === undefined) {}
}
import foo from './foo.js';

if (foo.bar !== undefined) {}

Options

checkGlobalVariables

Type: boolean
Default: false

This rule ignores variables not defined in file by default.

Set it to true to check all variables.

// eslint unicorn/no-typeof-undefined: ["error", {"checkGlobalVariables": true}]
if (typeof undefinedVariable === 'undefined') {} // Fails
// eslint unicorn/no-typeof-undefined: ["error", {"checkGlobalVariables": true}]
if (typeof Array === 'undefined') {}  // Fails