-
-
Notifications
You must be signed in to change notification settings - Fork 679
Expand file tree
/
Copy pathoptional-keys-of.d.ts
More file actions
46 lines (36 loc) · 1.13 KB
/
optional-keys-of.d.ts
File metadata and controls
46 lines (36 loc) · 1.13 KB
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
39
40
41
42
43
44
45
46
import type {IsOptionalKeyOf} from './is-optional-key-of.d.ts';
/**
Extract all optional keys from the given type.
This is useful when you want to create a new type that contains different type values for the optional keys only.
@example
```
import type {OptionalKeysOf, Except} from 'type-fest';
type User = {
name: string;
surname: string;
luckyNumber?: number;
};
const REMOVE_FIELD = Symbol('remove field symbol');
type UpdateOperation<Entity extends object> = Except<Partial<Entity>, OptionalKeysOf<Entity>> & {
[Key in OptionalKeysOf<Entity>]?: Entity[Key] | typeof REMOVE_FIELD;
};
const update1: UpdateOperation<User> = {
name: 'Alice',
};
const update2: UpdateOperation<User> = {
name: 'Bob',
luckyNumber: REMOVE_FIELD,
};
```
@category Utilities
*/
export type OptionalKeysOf<Type extends object> =
Type extends unknown // For distributing `Type`
? (keyof {[Key in keyof Type as
IsOptionalKeyOf<Type, Key> extends false
? never
: Key
]: never
}) & keyof Type // Intersect with `keyof Type` to ensure result of `OptionalKeysOf<Type>` is always assignable to `keyof Type`
: never; // Should never happen
export {};