-
-
Notifications
You must be signed in to change notification settings - Fork 679
Expand file tree
/
Copy pathrequired-deep.d.ts
More file actions
76 lines (67 loc) · 2.45 KB
/
required-deep.d.ts
File metadata and controls
76 lines (67 loc) · 2.45 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import type {BuiltIns, HasMultipleCallSignatures} from './internal/index.d.ts';
import type {IsNever} from './is-never.d.ts';
import type {Simplify} from './simplify.d.ts';
/**
Create a type from another type with all keys and nested keys set to required.
Use-cases:
- Creating optional configuration interfaces where the underlying implementation still requires all options to be fully specified.
- Modeling the resulting type after a deep merge with a set of defaults.
@example
```
import type {RequiredDeep} from 'type-fest';
type Settings = {
textEditor?: {
fontSize?: number;
fontColor?: string;
fontWeight?: number | undefined;
};
autocomplete?: boolean;
autosave?: boolean | undefined;
};
type RequiredSettings = RequiredDeep<Settings>;
//=> {
// textEditor: {
// fontSize: number;
// fontColor: string;
// fontWeight: number | undefined;
// };
// autocomplete: boolean;
// autosave: boolean | undefined;
// }
```
Note that types containing overloaded functions are not made deeply required due to a [TypeScript limitation](https://github.com/microsoft/TypeScript/issues/29732).
@category Utilities
@category Object
@category Array
@category Set
@category Map
*/
export type RequiredDeep<T> = T extends BuiltIns
? T
: T extends Map<infer KeyType, infer ValueType>
? Map<RequiredDeep<KeyType>, RequiredDeep<ValueType>>
: T extends Set<infer ItemType>
? Set<RequiredDeep<ItemType>>
: T extends ReadonlyMap<infer KeyType, infer ValueType>
? ReadonlyMap<RequiredDeep<KeyType>, RequiredDeep<ValueType>>
: T extends ReadonlySet<infer ItemType>
? ReadonlySet<RequiredDeep<ItemType>>
: T extends WeakMap<infer KeyType, infer ValueType>
? WeakMap<RequiredDeep<KeyType>, RequiredDeep<ValueType>>
: T extends WeakSet<infer ItemType>
? WeakSet<RequiredDeep<ItemType>>
: T extends Promise<infer ValueType>
? Promise<RequiredDeep<ValueType>>
: T extends (...arguments_: any[]) => unknown
? IsNever<keyof T> extends true
? T
: HasMultipleCallSignatures<T> extends true
? T
: ((...arguments_: Parameters<T>) => ReturnType<T>) & RequiredObjectDeep<T>
: T extends object
? Simplify<RequiredObjectDeep<T>> // `Simplify` to prevent `RequiredObjectDeep` from appearing in the resulting type
: unknown;
type RequiredObjectDeep<ObjectType extends object> = {
[KeyType in keyof ObjectType]-?: RequiredDeep<ObjectType[KeyType]>
};
export {};