Skip to main content

prefer-type-fest-set-optional

Require TypeFest SetOptional<T, Keys> over imported aliases like PartialBy.

Targeted pattern scopeโ€‹

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

  • Type references that resolve to imported PartialBy 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 SetOptional<T, Keys>.

  • Type references that resolve to imported PartialBy aliases.

Why this rule existsโ€‹

SetOptional is the canonical TypeFest utility for making selected keys optional. Standardizing on it improves discoverability and keeps utility naming consistent across projects.

โŒ Incorrectโ€‹

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

type PartialUser = PartialBy<User, "email">;

โœ… Correctโ€‹

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

type PartialUser = SetOptional<User, "email">;

Behavior and migration notesโ€‹

  • SetOptional<T, Keys> marks a selected subset of keys optional while preserving all other key modifiers.
  • This rule targets imported alias names that represent the same semantics (PartialBy).
  • Use this utility for draft/update shapes where only specific fields become optional.

Additional examplesโ€‹

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

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

type Draft = PartialBy<User, "email">;

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

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

type Draft = SetOptional<User, "email">;

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

type PartialAddress = SetOptional<Address, "line2">;

ESLint flat config exampleโ€‹

import typefest from "eslint-plugin-typefest";

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

When not to use itโ€‹

Disable this rule if external contracts require preserving existing aliases.

Package documentationโ€‹

TypeFest package documentation:

Source file: source/set-optional.d.ts

/**
Create a type that makes the given keys optional. The remaining keys are kept as is. The sister of the `SetRequired` type.

Use-case: You want to define a single model where the only thing that changes is whether or not some of the keys are optional.

@example
```
import type {SetOptional} from 'type-fest';

type Foo = {
a: number;
b?: string;
c: boolean;
};

type SomeOptional = SetOptional<Foo, 'b' | 'c'>;
// type SomeOptional = {
// a: number;
// b?: string; // Was already optional and still is.
// c?: boolean; // Is now optional.
// }
```

@category Object
*/

Rule catalog ID: R063

Further readingโ€‹

Adoption resourcesโ€‹