Skip to main content

prefer-type-fest-set-non-nullable

Require TypeFest SetNonNullable<T, Keys> over imported aliases like NonNullableBy.

Targeted pattern scopeโ€‹

This rule scopes matching to imported legacy aliases that model the same semantics as SetNonNullable<T, Keys>.

  • Type references that resolve to imported NonNullableBy aliases.

Locally defined lookalikes or unrelated type references are excluded unless they resolve to the targeted imported alias.

What this rule reportsโ€‹

This rule reports imported alias usages that should migrate to SetNonNullable<T, Keys>.

  • Type references that resolve to imported NonNullableBy aliases.

Why this rule existsโ€‹

SetNonNullable is the canonical TypeFest utility for making selected keys non-nullable. Standardizing on canonical TypeFest naming keeps utility usage predictable in public TypeScript codebases.

โŒ Incorrectโ€‹

import type { NonNullableBy } from "type-aliases";

type PersistedUser = NonNullableBy<User, "id">;

โœ… Correctโ€‹

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

type PersistedUser = SetNonNullable<User, "id">;

Behavior and migration notesโ€‹

  • SetNonNullable<T, Keys> enforces non-nullability on selected keys while preserving the rest of the shape.
  • This rule targets imported alias names that duplicate the same semantics (NonNullableBy).
  • Use this utility for persisted/entity states where selected fields must be present and non-null after validation.

Additional examplesโ€‹

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

import type { NonNullableBy } from "type-aliases";

type Persisted = NonNullableBy<User, "id">;

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

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

type Persisted = SetNonNullable<User, "id">;

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

type SafeOrder = SetNonNullable<Order, "orderId" | "createdAt">;

ESLint flat config exampleโ€‹

import typefest from "eslint-plugin-typefest";

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

When not to use itโ€‹

Disable this rule if existing exported aliases must remain stable.

Package documentationโ€‹

TypeFest package documentation:

Source file: source/set-non-nullable.d.ts

/**
* Create a type that makes the given keys non-nullable, where the remaining
* keys are kept as is.
*
* If no keys are given, all keys will be made non-nullable.
*
* Use-case: You want to define a single model where the only thing that changes
* is whether or not some or all of the keys are non-nullable.
*
* @category Object
*
* @example
* ```
* import type {SetNonNullable} from 'type-fest';
*
* type Foo = {
* a: number | null;
* b: string | undefined;
* c?: boolean | null;
* };
*
* type SomeNonNullable = SetNonNullable<Foo, 'b' | 'c'>;
* // type SomeNonNullable = {
* // a: number | null;
* // b: string; // Can no longer be undefined.
* // c?: boolean; // Can no longer be null, but is still optional.
* // }
*
* type AllNonNullable = SetNonNullable<Foo>;
* // type AllNonNullable = {
* // a: number; // Can no longer be null.
* // b: string; // Can no longer be undefined.
* // c?: boolean; // Can no longer be null, but is still optional.
* // }
* ```;
*/

Rule catalog ID: R062

Further readingโ€‹

Adoption resourcesโ€‹