-
-
Notifications
You must be signed in to change notification settings - Fork 677
Expand file tree
/
Copy pathreadonly-keys-of.d.ts
More file actions
38 lines (29 loc) · 888 Bytes
/
readonly-keys-of.d.ts
File metadata and controls
38 lines (29 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import type {IsReadonlyKeyOf} from './is-readonly-key-of.d.ts';
/**
Extract all readonly keys from the given type.
This is useful when you want to create a new type that contains readonly keys only.
@example
```
import type {ReadonlyKeysOf} from 'type-fest';
type User = {
name: string;
surname: string;
readonly id: number;
};
type UpdateResponse<Entity extends object> = Pick<Entity, ReadonlyKeysOf<Entity>>;
const update1: UpdateResponse<User> = {
id: 123,
};
```
@category Utilities
*/
export type ReadonlyKeysOf<Type extends object> =
Type extends unknown // For distributing `Type`
? (keyof {[Key in keyof Type as
IsReadonlyKeyOf<Type, Key> extends false
? never
: Key
]: never
}) & keyof Type // Intersect with `keyof Type` to ensure result of `ReadonlyKeysOf<Type>` is always assignable to `keyof Type`
: never; // Should never happen
export {};