Type Alias SetReadonly<BaseType, Keys>

SetReadonly: BaseType extends unknown
    ? Simplify<Except<BaseType, Keys> & Readonly<Pick<BaseType, Keys>>>
    : never

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.

Type Parameters

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.
// }