Skip to main content

prefer-type-fest-set-required

Require TypeFest SetRequired<T, Keys> over imported aliases like RequiredBy.

Targeted pattern scopeโ€‹

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

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

  • Type references that resolve to imported RequiredBy aliases.

Why this rule existsโ€‹

SetRequired is the canonical TypeFest utility for making selected keys required. Standardizing on TypeFest naming reduces semantic drift between utility libraries.

โŒ Incorrectโ€‹

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

type CompleteUser = RequiredBy<User, "id">;

โœ… Correctโ€‹

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

type CompleteUser = SetRequired<User, "id">;

Behavior and migration notesโ€‹

  • SetRequired<T, Keys> forces selected optional keys to be required.
  • This rule targets imported aliases with equivalent semantics (RequiredBy).
  • Use this utility in post-validation and persisted-domain types where selected fields become mandatory.

Additional examplesโ€‹

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

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

type Complete = RequiredBy<User, "id">;

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

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

type Complete = SetRequired<User, "id">;

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

type Persisted = SetRequired<Order, "id" | "status">;

ESLint flat config exampleโ€‹

import typefest from "eslint-plugin-typefest";

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

When not to use itโ€‹

Disable this rule if existing exported alias names are part of a compatibility contract.

Package documentationโ€‹

TypeFest package documentation:

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

/**
Create a type that makes the given keys required. The remaining keys are kept as is. The sister of the `SetOptional` 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 required.

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

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

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

// Set specific indices in an array to be required.
type ArrayExample = SetRequired<[number?, number?, number?], 0 | 1>;
//=> [number, number, number?]
```

@category Object
*/

Rule catalog ID: R065

Further readingโ€‹

Adoption resourcesโ€‹