-
Notifications
You must be signed in to change notification settings - Fork 642
/
Copy pathmaybe.ts
48 lines (40 loc) · 1.24 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
import {
union,
optional,
IType,
undefinedType,
nullType,
IAnyType,
assertIsType
} from "../../internal"
const optionalUndefinedType = optional(undefinedType, undefined)
const optionalNullType = optional(nullType, null)
/** @hidden */
export interface IMaybeIType<IT extends IAnyType, C, O>
extends IType<IT["CreationType"] | C, IT["SnapshotType"] | O, IT["TypeWithoutSTN"] | 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)
}