-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.ts
75 lines (64 loc) · 2.25 KB
/
types.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
64
65
66
67
68
69
70
71
72
73
74
75
export type SuccessResult<Success> = {
readonly tag: 'success'
readonly success: Success
}
export type FailureResult<Failure> = {
readonly tag: 'failure'
readonly failure: Failure
}
/** A value that represents either a success or a failure, including an associated value in each case. */
export type Result<Success = unknown, Failure = unknown> =
| SuccessResult<Success>
| FailureResult<Failure>
/**
* An async value that represents either a success or a failure,
* including an associated value in each case.
* */
export type AsyncResult<Success = unknown, Failure = unknown> = Promise<Result<Success, Failure>>
export type SomeResult<Success = unknown, Failure = unknown> =
| Result<Success, Failure>
| AsyncResult<Success, Failure>
/** Infers type of 'failure' value */
export type FailureOf<R extends SomeResult> = R extends Promise<infer AR>
? AR extends FailureResult<infer Failure>
? Failure
: never
: R extends FailureResult<infer Failure>
? Failure
: never
/** Infers type of 'success' value */
export type SuccessOf<R extends SomeResult> = R extends Promise<infer AR>
? AR extends SuccessResult<infer Success>
? Success
: never
: R extends SuccessResult<infer Success>
? Success
: never
type SuccessResultMatcher<Success, Match> = {
readonly success: (success: Success) => Match
readonly failure?: never
readonly default?: never
}
type FailureResultMatcher<Failure, Match> = {
readonly failure: (failure: Failure) => Match
readonly success?: never
readonly default?: never
}
export type ResultMatcher<Success, Failure, Match> =
| {
readonly success: (success: Success) => Match
readonly failure: (failure: Failure) => Match
readonly default?: never
}
| {
readonly success?: (success: Success) => NonNullable<Match>
readonly failure?: (failure: Failure) => NonNullable<Match>
readonly default: Match
}
export type Matcher<R extends SomeResult, Match> = FailureOf<R> extends never
? SuccessResultMatcher<SuccessOf<R>, Match>
: SuccessOf<R> extends never
? FailureResultMatcher<FailureOf<R>, Match>
: ResultMatcher<SuccessOf<R>, FailureOf<R>, Match>
export type CombineArray<T> = readonly T[]
export type CombineFunArray<Arg, Return> = CombineArray<(arg: Arg) => Return>