/
maybe.ts
63 lines (55 loc) · 1.5 KB
/
maybe.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
53
54
55
56
57
58
59
60
61
62
63
import {
union,
optional,
IType,
undefinedType,
nullType,
IAnyType,
ExtractC,
ExtractS,
ExtractT,
RedefineIStateTreeNode,
assertIsType,
ExtractNodeT
} from "../../internal"
const optionalUndefinedType = optional(undefinedType, undefined)
const optionalNullType = optional(nullType, null)
/** @hidden */
export interface IMaybeIType<IT extends IAnyType, C, O>
extends IType<
ExtractC<IT> | C,
ExtractS<IT> | O,
| RedefineIStateTreeNode<
ExtractT<IT>,
ExtractC<IT> | C,
ExtractS<IT> | O,
ExtractNodeT<ExtractT<IT>> | O
>
| O
> {}
/** @hidden */
export interface IMaybe<IT extends IAnyType> extends IMaybeIType<IT, undefined, undefined> {}
/** @hidden */
export interface IMaybeNull<IT extends IAnyType> extends IMaybeIType<IT, null | undefined, null> {}
/**
* `types.maybe` - Maybe will make a type nullable, and also optional.
* The value `undefined` will be used to represent nullability.
*
* @param type
* @returns
*/
export function maybe<IT extends IAnyType>(type: IT): IMaybe<IT> {
assertIsType(type, 1)
return union(type, optionalUndefinedType)
}
/**
* `types.maybeNull` - Maybe will make a type nullable, and also optional.
* The value `null` will be used to represent no value.
*
* @param type
* @returns
*/
export function maybeNull<IT extends IAnyType>(type: IT): IMaybeNull<IT> {
assertIsType(type, 1)
return union(type, optionalNullType)
}