forked from sindresorhus/type-fest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set-non-nullable.d.ts
39 lines (32 loc) · 1.07 KB
/
set-non-nullable.d.ts
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
/**
Create a type that makes the given keys non-nullable, where the remaining keys are kept as is.
If no keys are given, all keys will be made non-nullable.
Use-case: You want to define a single model where the only thing that changes is whether or not some or all of the keys are non-nullable.
@example
```
import type {SetNonNullable} from 'type-fest';
type Foo = {
a: number | null;
b: string | undefined;
c?: boolean | null;
}
type SomeNonNullable = SetNonNullable<Foo, 'b' | 'c'>;
// type SomeNonNullable = {
// a: number | null;
// b: string; // Can no longer be undefined.
// c?: boolean; // Can no longer be null, but is still optional.
// }
type AllNonNullable = SetNonNullable<Foo>;
// type AllNonNullable = {
// a: number; // Can no longer be null.
// b: string; // Can no longer be undefined.
// c?: boolean; // Can no longer be null, but is still optional.
// }
```
@category Object
*/
export type SetNonNullable<BaseType, Keys extends keyof BaseType = keyof BaseType> = {
[Key in keyof BaseType]: Key extends Keys
? NonNullable<BaseType[Key]>
: BaseType[Key];
};