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
ReadonlyByaliases.
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
ReadonlyByaliases.
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.
@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.
// }
```
@category Object
*/
Rule catalog ID: R064