Skip to main content

prefer-ts-extras-is-present

Require isPresent from ts-extras for direct nullish checks outside Array.prototype.filter callbacks.

Targeted pattern scopeโ€‹

This rule scopes matching to direct nullish-check expressions outside .filter(...) callbacks.

  • Direct nullish checks outside Array.prototype.filter callbacks:
  • value != null
  • value == null
  • value !== null && value !== undefined
  • value === null || value === undefined

Filter callbacks are handled by the dedicated filter rule; larger boolean expressions are only matched when they keep these shapes.

What this rule reportsโ€‹

This rule reports direct nullish comparisons that should use isPresent(...) helpers.

  • Direct nullish checks outside Array.prototype.filter callbacks:
  • value != null
  • value == null
  • value !== null && value !== undefined
  • value === null || value === undefined

Why this rule existsโ€‹

isPresent gives one canonical predicate for non-nullish checks and reduces mixed null/undefined comparison styles.

  • Nullish guard intent is explicit.
  • Narrowing to NonNullable<T> follows one convention.
  • Verbose inline nullish checks are removed.

โŒ Incorrectโ€‹

if (value != null) {
consume(value);
}

if (value === null || value === undefined) {
return;
}

โœ… Correctโ€‹

if (isPresent(value)) {
consume(value);
}

if (!isPresent(value)) {
return;
}

Behavior and migration notesโ€‹

  • isPresent(value) means value is neither null nor undefined.
  • !isPresent(value) is the nullish guard equivalent.
  • Filter-specific nullish patterns are covered by prefer-ts-extras-is-present-filter.

Additional examplesโ€‹

โŒ Incorrect โ€” Additional exampleโ€‹

if (profile != null) {
render(profile);
}

โœ… Correct โ€” Additional exampleโ€‹

if (isPresent(profile)) {
render(profile);
}

โœ… Correct โ€” Repository-wide usageโ€‹

const available = isPresent(cacheEntry);

ESLint flat config exampleโ€‹

import typefest from "eslint-plugin-typefest";

export default [
{
plugins: { typefest },
rules: {
"typefest/prefer-ts-extras-is-present": "error",
},
},
];

When not to use itโ€‹

Disable this rule if your code style requires explicit === null / === undefined branches.

Package documentationโ€‹

ts-extras package documentation:

Source file: source/is-present.ts

/**
Check whether a value is present (non-nullable), meaning it is neither `null` nor `undefined`.

This can be useful as a type guard, as for example, `[1, null].filter(Boolean)` does not always type-guard correctly.

@example
```
import {isPresent} from 'ts-extras';

[1, null, 2, undefined].filter(isPresent);
//=> [1, 2]
```

@category Type guard
*/

Rule catalog ID: R021

Further readingโ€‹

Adoption resourcesโ€‹