Skip to main content

prefer-type-fest-optional

Require TypeFest Optional<Value> over Exclude<T, null> | undefined and NonNullable<T> | undefined patterns.

Targeted pattern scope​

This rule targets optional-value patterns that normalize null away and add undefined.

What this rule reports​

  • Exclude<T, null> | undefined
  • Exclude<T, null | undefined> | undefined
  • NonNullable<T> | undefined

Why this rule exists​

Optional<Value> is the canonical TypeFest helper for β€œvalue or undefined, but never null”. Using it makes the intent obvious and standardizes on the new TypeFest utility.

❌ Incorrect​

type MaybeName = Exclude<string | null, null> | undefined;

βœ… Correct​

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

type MaybeName = Optional<string | null>;

Behavior and migration notes​

  • This rule only targets explicit null-stripping plus undefined patterns.
  • It does not report plain T | undefined unions.
  • It will not autofix when Optional is shadowed in the local type scope.

Additional examples​

❌ Incorrect β€” Additional example​

type MaybeRegion = NonNullable<string | null> | undefined;

βœ… Correct β€” Additional example​

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

type MaybeRegion = Optional<string | null>;

βœ… Correct β€” Non-targeted usage​

type MaybeRegion = string | undefined;

ESLint flat config example​

import typefest from "eslint-plugin-typefest";

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

When not to use it​

Disable this rule if your codebase intentionally prefers native utility combinations over the TypeFest helper name.

Package documentation​

TypeFest package documentation:

Source file: source/optional.d.ts

/**
Create a type that represents either the value or `undefined`, while stripping `null` from the type.
*/
export type Optional<Value> = Exclude<Value, null> | undefined;

Rule catalog ID: R079

Further reading​

Adoption resources​