forked from ts-essentials/ts-essentials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
36 lines (35 loc) · 1.44 KB
/
index.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
import { Builtin } from "../built-in";
import { IsTuple } from "../is-tuple";
export type DeepNullable<Type> = Type extends Builtin
? Type | null
: Type extends Map<infer Keys, infer Values>
? Map<DeepNullable<Keys>, DeepNullable<Values>>
: Type extends ReadonlyMap<infer Keys, infer Values>
? ReadonlyMap<DeepNullable<Keys>, DeepNullable<Values>>
: Type extends WeakMap<infer Keys, infer Values>
? // TODO: replace it with WeakKey (introduced at [email protected])
// WeakMap key has to satisfy WeakKey which is object at the moment
DeepNullable<Keys> extends object
? WeakMap<DeepNullable<Keys>, DeepNullable<Values>>
: never
: Type extends Set<infer Values>
? Set<DeepNullable<Values>>
: Type extends ReadonlySet<infer Values>
? ReadonlySet<DeepNullable<Values>>
: Type extends WeakSet<infer Values>
? // TODO: replace it with WeakKey (introduced at [email protected])
// WeakSet key has to satisfy WeakKey which is object at the moment
DeepNullable<Values> extends object
? WeakSet<DeepNullable<Values>>
: never
: Type extends ReadonlyArray<infer Values>
? Type extends IsTuple<Type>
? { [Key in keyof Type]: DeepNullable<Type[Key]> | null }
: Type extends Array<Values>
? Array<DeepNullable<Values>>
: ReadonlyArray<DeepNullable<Values>>
: Type extends Promise<infer Value>
? Promise<DeepNullable<Value>>
: Type extends {}
? { [Key in keyof Type]: DeepNullable<Type[Key]> }
: Type | null;