prefer-ts-extras-is-defined
Require isDefined from ts-extras for direct undefined checks outside Array.prototype.filter callbacks.
Targeted pattern scopeโ
This rule scopes matching to direct undefined-check expressions outside .filter(...) callbacks.
- Direct undefined checks outside
Array.prototype.filtercallbacks: value !== undefinedundefined !== valuetypeof value !== "undefined"value === undefined
Loose comparisons (== / !=) are intentionally ignored because they also match null, which is not equivalent to isDefined(...).
Filter callbacks are handled by the dedicated filter rule; broader boolean expressions are only matched when they keep these forms.
What this rule reportsโ
This rule reports direct undefined comparisons that should use isDefined(...) helpers.
- Direct undefined checks outside
Array.prototype.filtercallbacks: value !== undefinedundefined !== valuetypeof value !== "undefined"value === undefined
Why this rule existsโ
isDefined turns repeated undefined comparisons into one canonical predicate.
- Guard intent is explicit at call sites.
- Narrowing style is consistent across modules.
- Repeated inline comparison variants are removed.
โ Incorrectโ
if (value !== undefined) {
consume(value);
}
if (value === undefined) {
return;
}
โ Correctโ
if (isDefined(value)) {
consume(value);
}
if (!isDefined(value)) {
return;
}
Behavior and migration notesโ
isDefined(value)is equivalent tovalue !== undefined.!isDefined(value)is equivalent tovalue === undefined.- Loose
value != undefinedandvalue == undefinedchecks are not auto-fixed by this rule because replacing them withisDefinedwould change runtime behavior fornull. - Autofix is intentionally skipped for comparisons whose value resolves to
@typescript-eslintAST node types (for exampleTSESTree.Node | undefined) to avoid pathological type-analysis performance cliffs in downstream lint passes. - Filter-specific patterns are intentionally covered by
prefer-ts-extras-is-defined-filter.
Additional examplesโ
โ Incorrect โ Additional exampleโ
if (sessionId !== undefined) {
connect(sessionId);
}
โ Correct โ Additional exampleโ
if (isDefined(sessionId)) {
connect(sessionId);
}
โ Correct โ Repository-wide usageโ
const hasValue = isDefined(input);
ESLint flat config exampleโ
import typefest from "eslint-plugin-typefest";
export default [
{
plugins: { typefest },
rules: {
"typefest/prefer-ts-extras-is-defined": "error",
},
},
];
When not to use itโ
Disable this rule if your team uses explicit === undefined comparisons as a required style convention.
Package documentationโ
ts-extras package documentation:
Source file: source/is-defined.ts
/**
Check whether a value is defined, meaning it is not `undefined`.
This can be useful as a type guard, as for example, `[1, undefined].filter(Boolean)` does not always type-guard correctly.
@example
```
import {isDefined} from 'ts-extras';
[1, undefined, 2].filter(isDefined);
//=> [1, 2]
```
@category Type guard
*/
Rule catalog ID: R014