UNPKG

637 BTypeScriptView Raw
1import type {IsEqual} from './is-equal';
2
3/**
4Extract all readonly keys from the given type.
5
6This is useful when you want to create a new type that contains readonly keys only.
7
8@example
9```
10import type {ReadonlyKeysOf} from 'type-fest';
11
12interface User {
13 name: string;
14 surname: string;
15 readonly id: number;
16}
17
18type UpdateResponse<Entity extends object> = Pick<Entity, ReadonlyKeysOf<Entity>>;
19
20const update1: UpdateResponse<User> = {
21 id: 123,
22};
23```
24
25@category Utilities
26*/
27export type ReadonlyKeysOf<T> = NonNullable<{
28 [P in keyof T]: IsEqual<{[Q in P]: T[P]}, {readonly [Q in P]: T[P]}> extends true ? P : never
29}[keyof T]>;