/
Compulsory.ts
52 lines (47 loc) 路 1.25 KB
/
Compulsory.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
40
41
42
43
44
45
46
47
48
49
50
51
52
import {_Pick} from './Pick'
import {Depth} from './_Internal'
import {Key} from '../Any/Key'
import {NonNullable} from '../Union/NonNullable'
import {PatchFlat} from './Patch'
import {BuiltIn} from '../Misc/BuiltIn'
/**
* @hidden
*/
export type CompulsoryFlat<O> = {
[K in keyof O]-?: NonNullable<O[K]>
} & {}
/**
* @hidden
*/
export type CompulsoryDeep<O> = {
[K in keyof O]-?: O[K] extends BuiltIn
? O[K]
: CompulsoryDeep<NonNullable<O[K]>>
}
/**
* @hidden
*/
export type CompulsoryPart<O extends object, depth extends Depth> = {
'flat': CompulsoryFlat<O>,
'deep': CompulsoryDeep<O>,
}[depth]
/**
* @hidden
*/
export type _Compulsory<O extends object, K extends Key, depth extends Depth> =
PatchFlat<CompulsoryPart<_Pick<O, K>, depth>, O>
/**
* Make that `O`'s fields cannot be [[Nullable]] or [[Optional]] (it's like
* [[Required]] & [[NonNullable]] at once).
* @param O to make compulsory
* @param K (?=`Key`) to choose fields
* @param depth (?=`'flat'`) 'deep' to do it deeply
* @returns [[Object]]
* @example
* ```ts
* ```
*/
export type Compulsory<O extends object, K extends Key = Key, depth extends Depth = 'flat'> =
O extends unknown
? _Compulsory<O, K, depth>
: never