-
Notifications
You must be signed in to change notification settings - Fork 328
/
Copy pathCodec.ts
122 lines (94 loc) · 2.58 KB
/
Codec.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import * as _ from '../../src/Codec'
declare const NumberFromString: _.Codec<string, string, number>
//
// fromStruct
//
// $ExpectType Codec<{ a: unknown; b: { c: string; }; }, { a: string; b: { c: string; }; }, { a: string; b: { c: number; }; }>
_.fromStruct({
a: _.string,
b: _.fromStruct({
c: NumberFromString
})
})
//
// struct
//
// $ExpectType Codec<unknown, { a: string; b: { c: number; }; }, { a: string; b: { c: number; }; }>
_.struct({
a: _.string,
b: _.struct({
c: _.number
})
})
//
// fromPartial
//
// $ExpectType Codec<Partial<{ a: unknown; b: Partial<{ c: string; }>; }>, Partial<{ a: string; b: Partial<{ c: string; }>; }>, Partial<{ a: string; b: Partial<{ c: number; }>; }>>
_.fromPartial({
a: _.string,
b: _.fromPartial({
c: NumberFromString
})
})
//
// partial
//
// $ExpectType Codec<unknown, Partial<{ a: string; b: Partial<{ c: number; }>; }>, Partial<{ a: string; b: Partial<{ c: number; }>; }>>
_.partial({
a: _.string,
b: _.partial({
c: _.number
})
})
//
// fromArray
//
// $ExpectType Codec<string[], string[], number[]>
_.fromArray(NumberFromString)
//
// array
//
// $ExpectType Codec<unknown, string[], string[]>
_.array(_.string)
//
// fromRecord
//
// $ExpectType Codec<Record<string, string>, Record<string, string>, Record<string, number>>
_.fromRecord(NumberFromString)
//
// record
//
// $ExpectType Codec<unknown, Record<string, string>, Record<string, string>>
_.record(_.string)
//
// fromTuple
//
// $ExpectType Codec<[unknown, string, unknown], [string, string, boolean], [string, number, boolean]>
_.fromTuple(_.string, NumberFromString, _.boolean)
//
// tuple
//
// $ExpectType Codec<unknown, [string, number, boolean], [string, number, boolean]>
_.tuple(_.string, _.number, _.boolean)
//
// fromSum
//
// $ExpectType Codec<{ _tag: unknown; a: unknown; } | { _tag: unknown; b: string; }, { _tag: "A"; a: string; } | { _tag: "B"; b: string; }, { _tag: "A"; a: string; } | { _tag: "B"; b: number; }>
_.fromSum('_tag')({
A: _.fromStruct({ _tag: _.literal('A'), a: _.string }),
B: _.fromStruct({ _tag: _.literal('B'), b: NumberFromString })
})
//
// sum
//
const S1 = _.struct({ _tag: _.literal('A'), a: _.string })
const S2 = _.struct({ _tag: _.literal('B'), b: _.number })
// $ExpectType Codec<unknown, { _tag: "A"; a: string; } | { _tag: "B"; b: number; }, { _tag: "A"; a: string; } | { _tag: "B"; b: number; }>
_.sum('_tag')({ A: S1, B: S2 })
// // $ExpectError
// _.sum('_tag')({ A: S1, B: S1 })
//
// optional
//
// $ExpectType Codec<string | undefined, string | undefined, Option<number>>
_.optional(NumberFromString)