forked from gcanti/io-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathType.ts
224 lines (194 loc) · 5.31 KB
/
Type.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
* **This module is experimental**
*
* Experimental features are published in order to get early feedback from the community, see these tracking
* [issues](https://github.com/gcanti/io-ts/issues?q=label%3Av2.2+) for further discussions and enhancements.
*
* A feature tagged as _Experimental_ is in a high state of flux, you're at risk of it changing without notice.
*
* @since 2.2.3
*/
import * as E from 'fp-ts/lib/Either'
import { pipe } from 'fp-ts/lib/pipeable'
import * as t from './index'
import * as S from './Schemable'
// -------------------------------------------------------------------------------------
// model
// -------------------------------------------------------------------------------------
/**
* @category model
* @since 2.2.3
*/
export interface Type<A> extends t.Type<A, unknown, unknown> {}
// -------------------------------------------------------------------------------------
// constructors
// -------------------------------------------------------------------------------------
/**
* @category constructors
* @since 2.2.3
*/
export const literal = <A extends readonly [S.Literal, ...Array<S.Literal>]>(...values: A): Type<A[number]> =>
t.union(values.map((v) => t.literal(v as any)) as any)
// -------------------------------------------------------------------------------------
// primitives
// -------------------------------------------------------------------------------------
/**
* @category primitives
* @since 2.2.3
*/
export const string: Type<string> = t.string
/**
* @category primitives
* @since 2.2.3
*/
export const number: Type<number> = new t.Type(
t.number.name,
t.number.is,
(u, c) =>
pipe(
t.number.decode(u),
E.chain((n) => (isNaN(n) ? t.failure(u, c) : t.success(n)))
),
t.number.encode
)
/**
* @category primitives
* @since 2.2.3
*/
export const boolean: Type<boolean> = t.boolean
/**
* @category primitives
* @since 2.2.3
*/
export const UnknownArray: Type<Array<unknown>> = t.UnknownArray
/**
* @category primitives
* @since 2.2.3
*/
export const UnknownRecord: Type<Record<string, unknown>> = t.UnknownRecord
// -------------------------------------------------------------------------------------
// combinators
// -------------------------------------------------------------------------------------
/**
* @category combinators
* @since 2.2.3
*/
export const refine = <A, B extends A>(refinement: (a: A) => a is B, id: string) => (from: Type<A>): Type<B> =>
// tslint:disable-next-line: deprecation
t.refinement(from, refinement, id) as any
/**
* @category combinators
* @since 2.2.3
*/
export const nullable = <A>(or: Type<A>): Type<null | A> => t.union([t.null, or])
/**
* @category combinators
* @since 2.2.3
*/
export const type = <A>(properties: { [K in keyof A]: Type<A[K]> }): Type<{ [K in keyof A]: A[K] }> =>
t.type(properties) as any
/**
* @category combinators
* @since 2.2.3
*/
export const partial = <A>(properties: { [K in keyof A]: Type<A[K]> }): Type<Partial<{ [K in keyof A]: A[K] }>> =>
t.partial(properties)
/**
* @category combinators
* @since 2.2.3
*/
export const record = <A>(codomain: Type<A>): Type<Record<string, A>> => t.record(t.string, codomain)
/**
* @category combinators
* @since 2.2.3
*/
export const array = <A>(item: Type<A>): Type<Array<A>> => t.array(item)
/**
* @category combinators
* @since 2.2.3
*/
export const tuple = <A extends ReadonlyArray<unknown>>(...components: { [K in keyof A]: Type<A[K]> }): Type<A> =>
t.tuple(components as any) as any
/**
* @category combinators
* @since 2.2.3
*/
export const intersect = <B>(right: Type<B>) => <A>(left: Type<A>): Type<A & B> => t.intersection([left, right])
/**
* @category combinators
* @since 2.2.3
*/
export const lazy = <A>(id: string, f: () => Type<A>): Type<A> => t.recursion(id, f)
/**
* @category combinators
* @since 2.2.3
*/
export const sum = <T extends string>(_tag: T) => <A>(members: { [K in keyof A]: Type<A[K]> }): Type<A[keyof A]> =>
t.union(Object.values(members) as any)
/**
* @category combinators
* @since 2.2.3
*/
export const union = <A extends readonly [unknown, ...Array<unknown>]>(
...members: { [K in keyof A]: Type<A[K]> }
): Type<A[number]> => t.union(members as any)
// -------------------------------------------------------------------------------------
// instances
// -------------------------------------------------------------------------------------
/**
* @category instances
* @since 2.2.3
*/
export const URI = 'io-ts/Type'
/**
* @category instances
* @since 2.2.3
*/
export type URI = typeof URI
declare module 'fp-ts/lib/HKT' {
interface URItoKind<A> {
readonly [URI]: Type<A>
}
}
/**
* @category instances
* @since 2.2.8
*/
export const Schemable: S.Schemable1<URI> = {
URI,
literal,
string,
number,
boolean,
nullable,
type,
partial,
record,
array,
tuple: tuple as S.Schemable1<URI>['tuple'],
intersect,
sum,
lazy
}
/**
* @category instances
* @since 2.2.8
*/
export const WithUnknownContainers: S.WithUnknownContainers1<URI> = {
UnknownArray,
UnknownRecord
}
/**
* @category instances
* @since 2.2.8
*/
export const WithUnion: S.WithUnion1<URI> = {
union: union as S.WithUnion1<URI>['union']
}
/**
* @category instances
* @since 2.2.8
*/
export const WithRefine: S.WithRefine1<URI> = {
refine: refine as S.WithRefine1<URI>['refine']
}