Skip to content

nullishCheckStyle

Enforces consistent equality operator usage when comparing with null or undefined.

✅ This rule is included in the ts preset.

When testing a value for being a nullish value (null or undefined), one can test for each nullish value separately using the strict equality operators (===/!==):

foo === null; // true if foo is null
bar !== undefined; // true if bar is anything other undefined

Or, one can conveniently test for both at the same time, using the loose equality operators (==/!=):

foo == null; // true if foo is null OR undefined
foo == undefined; // same as previous line
foo != null; // true if foo is NEITHER null NOR undefined
foo != undefined; // same as previous line

This rule enforces that a consistent style is used for null/undefined checks.

This option has three possible values: "double-equals" (default), ignore, and triple-equals.

This enforces that null/undefined checks are done using the loose equality operators, i.e. == null/== undefined.

"double-equals" is recommended since most code does not need to care about whether a nullish value is specifically null or specifically undefined. This mirrors the behavior of the built-in language features such as ?. and ??, which do not distinguish between nullish values. Testing for both values at once also means that null/undefined checks are less likely to be impacted when refactoring.

Examples of code with the "double-equals" option:

if (x === null) {
// code...
}
if (foo !== undefined) {
// code...
}

This option enforces that the strict equality operators are used for null/undefined checks.

Examples of code with the "triple-equals" option:

if (x == null) {
// code...
}
if (foo != undefined) {
// code...
}

This option does not enforce any preference between == and === in null/undefined checks.

Examples of valid code with this option:

if (x == null) {
}
if (foo === null) {
}

Type: "prefer-null" | "prefer-undefined"| "ignore" Default: "prefer-null"

Since x == null and x == undefined are equivalent, this option allows enforcing that the same choice is used consistently.

This is the default simply because == null is 5 characters shorter than == undefined.

Examples of code with { looseNullishComparisonStyle: "prefer-null" }:

if (x == undefined) {
// code...
}
if (foo != undefined) {
// code...
}

Examples of code with { looseNullishComparisonStyle: "prefer-null" }:

if (x == null) {
// code...
}
if (foo != null) {
// code...
}

Examples of correct code with { looseNullishComparisonStyle: "ignore" }:

if (x == undefined) {
}
if (foo == null) {
}

If you do not care about ensuring null/undefined checks are performed consistently in your codebase, you can safely disable this rule.

  • equalityOperators - Enforces consistent equality operator usage for scenarios other than null/undefined checks.
Made with ❤️‍🔥 around the world by the Flint team and contributors.