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> | undefinedExclude<T, null | undefined> | undefinedNonNullable<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
undefinedpatterns. - It does not report plain
T | undefinedunions. - It will not autofix when
Optionalis 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