Skip to main content

prefer-type-fest-set-readonly

Require TypeFest SetReadonly<T, Keys> over imported aliases like ReadonlyBy.

Targeted pattern scopeโ€‹

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

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

  • Type references that resolve to imported ReadonlyBy aliases.

Why this rule existsโ€‹

SetReadonly is the canonical TypeFest utility for making selected keys readonly. Canonical naming improves discoverability and consistency across projects.

โŒ Incorrectโ€‹

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

type ImmutableUser = ReadonlyBy<User, "id">;

โœ… Correctโ€‹

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

type ImmutableUser = SetReadonly<User, "id">;

Behavior and migration notesโ€‹

  • SetReadonly<T, Keys> applies readonly modifiers to selected keys only.
  • This rule targets imported aliases with equivalent semantics (ReadonlyBy).
  • Use it for immutable identity fields while keeping the rest of a shape mutable.

Additional examplesโ€‹

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

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

type Frozen = ReadonlyBy<User, "id">;

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

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

type Frozen = SetReadonly<User, "id">;

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

type ImmutableAudit = SetReadonly<AuditEntry, "timestamp" | "actor">;

ESLint flat config exampleโ€‹

import typefest from "eslint-plugin-typefest";

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

When not to use itโ€‹

Disable this rule if existing exported alias names must be preserved.

Package documentationโ€‹

TypeFest package documentation:

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

/**
* Create a type that makes the given keys readonly. The remaining keys are kept
* as is.
*
* Use-case: You want to define a single model where the only thing that changes
* is whether or not some of the keys are readonly.
*
* @category Object
*
* @example
* ```
* import type {SetReadonly} from 'type-fest';
*
* type Foo = {
* a: number;
* readonly b: string;
* c: boolean;
* };
*
* type SomeReadonly = SetReadonly<Foo, 'b' | 'c'>;
* // type SomeReadonly = {
* // a: number;
* // readonly b: string; // Was already readonly and still is.
* // readonly c: boolean; // Is now readonly.
* // }
* ```;
*/

Rule catalog ID: R064

Further readingโ€‹

Adoption resourcesโ€‹