-
-
Notifications
You must be signed in to change notification settings - Fork 710
Expand file tree
/
Copy pathunwrap-required.d.ts
More file actions
37 lines (28 loc) · 1.05 KB
/
Copy pathunwrap-required.d.ts
File metadata and controls
37 lines (28 loc) · 1.05 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
import type {MapsSetsOrArrays, NonRecursiveType} from './internal/type.d.ts';
/**
Revert the `Required` modifier on an object type.
Use-case: Infer the underlying type `T` when only `Required<T>` is available or the original type may not be directly accessible.
@example
```
import type {UnwrapRequired} from 'type-fest';
type ContactFormData = Required<{
email: string;
message?: string;
}>;
type DraftContactFormData = UnwrapRequired<ContactFormData>;
//=> {email: string; message?: string}
```
Note:
- If the provided type isn’t of the form `Required<T>`, `UnwrapRequired` simply returns the input type.
- `UnwrapRequired` doesn't work with arrays, if instantiated with arrays, it simply returns the input type.
@category Object
*/
export type UnwrapRequired<RequiredObjectType> =
RequiredObjectType extends NonRecursiveType | MapsSetsOrArrays
? RequiredObjectType
: _UnwrapRequired<RequiredObjectType>;
type _UnwrapRequired<RequiredObjectType> =
RequiredObjectType extends Required<infer ObjectType>
? ObjectType
: RequiredObjectType;
export {};