Skip to main content

prefer-type-fest-is-nullable

Require TypeFest IsNullable<T> over equivalent any-safe nullable conditional type guards.

Targeted pattern scopeโ€‹

This rule reports exact nullable guards that preserve TypeFest's any behavior:

  • IsAny<T> extends true ? true : Extract<T, null> extends never ? false : true
  • 0 extends 1 & T ? true : Extract<T, null> extends never ? false : true

It does not report the shorter Extract<T, null> extends never ? false : true form because that returns a different result for any.

What this rule reportsโ€‹

This rule reports manual null-membership checks that can be replaced by IsNullable<T> without losing TypeFest's any handling.

Why this rule existsโ€‹

IsNullable<T> makes nullable intent explicit and avoids repeating a nested conditional helper every time a type needs to detect null.

โŒ Incorrectโ€‹

import type { IsAny } from "type-fest";

type Result<T> = IsAny<T> extends true ? true : Extract<T, null> extends never ? false : true;

โœ… Correctโ€‹

import type { IsNullable } from "type-fest";

type Result<T> = IsNullable<T>;

Behavior and migration notesโ€‹

  • IsAny<T> must be imported from type-fest for the imported-helper pattern to report.
  • The manual 0 extends 1 & T any guard is also supported.
  • The inner check must be exactly Extract<T, null> extends never ? false : true.
  • Autofix is skipped when IsNullable is shadowed in the local scope.

Additional examplesโ€‹

โŒ Incorrect โ€” Manual any guardโ€‹

type Result<T> = 0 extends 1 & T ? true : Extract<T, null> extends never ? false : true;

โœ… Correct โ€” Manual any guardโ€‹

type Result<T> = IsNullable<T>;

โœ… Correct โ€” Not any-safeโ€‹

type Result<T> = Extract<T, null> extends never ? false : true;

ESLint flat config exampleโ€‹

import typefest from "eslint-plugin-typefest";

export default [
{
plugins: { typefest },
rules: {
"typefest/prefer-type-fest-is-nullable": "error",
},
},
];

When not to use itโ€‹

Disable this rule if a public helper must keep a hand-written nullable conditional for compatibility or teaching reasons.

Package documentationโ€‹

TypeFest package documentation:

Source file: source/is-nullable.d.ts

Rule catalog ID: R116

Further readingโ€‹

Adoption resourcesโ€‹