Skip to content

Latest commit

 

History

History
57 lines (40 loc) · 841 Bytes

File metadata and controls

57 lines (40 loc) · 841 Bytes

typelab / utils / ObjectReadonly

type ObjectReadonly<T, K> = IsObjectLiteral<T> extends true ? Detailed<{ readonly [Key in K]: T[K] } & { [Key in keyof T as Key extends K ? never : Key]: T[Key] }> : never;

Extended TypeScript `Readonly` to enforce specific properties as readonly.

Type Parameters

Type Parameter Description

T

The original object type.

K extends keyof T

The keys of T that should be readonly.

Returns

A type with the specified keys of T set as readonly.

Example

type Obj = { a: string; b: string };

// { readonly a: string; b: string }
type ReadonlyObj = ObjectReadonly<Obj, 'a'>;