-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
330 lines (296 loc) · 7.88 KB
/
test.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import {t, s} from './index';
namespace meta {
// test if 2 types are the same
export type equal<A, B> = A extends B ? B extends A ? true : false : false;
export const equal = <A, B>(_true: equal<A, B>) => {}
namespace test {
equal<{}, {}>(true);
equal<number[], [number, number]>(false);
equal<true, false>(false);
equal<number, 1>(false);
equal<string, ''>(false);
equal<'', ''>(true);
}
}
{
const the_true = true;
type T = t.TSType<typeof the_true>;
meta.equal<T, any>(true);
}
{
const the_false = false;
type T = t.TSType<typeof the_false>;
}
{
const the_string = s.string({
'default': 'test',
});
type T = t.TSType<typeof the_string>;
meta.equal<T, string>(true);
((_x: T) => {})('hello');
}
{
const the_number = s.number();
type T = t.TSType<typeof the_number>;
meta.equal<T, number>(true);
((_x: T) => {})(1);
}
{
const the_number_enum = s.number<1 | 2>({'enum': [1, 2]});
type T = t.TSType<typeof the_number_enum>;
meta.equal<T, 1 | 2>(true);
((_x: T) => {})(1);
((_x: T) => {})(2);
}
{
const the_integer = s.number({'title': 'integer'});
type T = t.TSType<typeof the_integer>;
meta.equal<T, number>(true);
((_x: T) => {})(1);
}
{
const the_integer_enum = s.number<1 | 2>({'enum': [1, 2]});
type T = t.TSType<typeof the_integer_enum>;
meta.equal<T, 1 | 2>(true);
((_x: T) => {})(1);
((_x: T) => {})(2);
}
{
const the_boolean = s.boolean({'description': 'this is a boolean', 'default': false});
type T = t.TSType<typeof the_boolean>;
meta.equal<T, boolean>(true);
((_x: T) => {})(true);
((_x: T) => {})(false);
}
{
const the_boolean_enum = s.boolean<true | true>({'enum': [true]});
type T = t.TSType<typeof the_boolean_enum>;
meta.equal<T, true>(true);
((_x: T) => {})(true);
}
{
const the_array = s.array({
'default': [],
'items': s.string(),
});
type T = t.TSType<typeof the_array>;
type a = T[number];
meta.equal<a, string>(true);
meta.equal<T, string[]>(true);
meta.equal<T, number[]>(false);
meta.equal<T, [any, any]>(false);
((_x: T) => {})([]);
}
{
const the_array = s.array({
'items': true,
});
type T = t.TSType<typeof the_array>;
type a = T[number];
meta.equal<a, any>(true);
meta.equal<T, any[]>(true);
// any extends string, and string extends any
meta.equal<T, string[]>(true);
meta.equal<T, [any, any]>(false);
((_x: T) => {})([]);
}
{
const the_array = s.array({
'items': false,
});
type T = t.TSType<typeof the_array>;
type a = T[number];
// FIXME: how to test never?
}
{
// FIXME: s.object() or s.object({}) doesn't work
const the_object = s.object({
'properties': {}
});
type T = t.TSType<typeof the_object>;
meta.equal<T, {}>(true);
// meta.equal<T, {[key: string]: number}>(false);
meta.equal<T, any[]>(false);
meta.equal<T, string>(false);
((_x: T) => {})({});
}
{
const the_object = s.object({
'properties': {
}
});
type T = t.TSType<typeof the_object>;
meta.equal<T, {}>(true);
// meta.equal<T, {[key: string]: number}>(false);
meta.equal<T, any[]>(false);
meta.equal<T, string>(false);
((_x: T) => {})({});
}
{
const the_object = s.object({
'default': {},
'properties': {
'hello': s.number(),
}
});
type T = t.TSType<typeof the_object>;
meta.equal<T, {hello: number}>(true);
meta.equal<T, {[key: string]: number}>(false);
meta.equal<T, any[]>(false);
meta.equal<T, string>(false);
((_x: T) => {})({'hello': 1});
}
{
const items = s.items();
const tuple = s.tuple({'items': items});
type T = t.TSType<typeof tuple>;
meta.equal<T, []>(true);
meta.equal<T, any[]>(false);
((_x: T) => {})([]);
}
{
const items = s.items(
s.string(),
s.number(),
s.integer(),
s.boolean(),
// FIXME: s.object() doesn't work
s.object({"properties": {}}),
s.object({
'properties': {
'hello': s.string()
}
}),
true,
// false,
);
const tuple = s.tuple({'items': items});
type T = t.TSType<typeof tuple>;
((_x: T) => {})([
'a',
2,
2,
true,
{},
{'hello': 'haha'},
['asas', 1, true, false, {}],
]);
}
{
// FIXME: s.object() doesn't work
const items = s.items(
s.object({
'properties': {}
}),
);
const tuple = s.tuple({'items': items});
type T = t.TSType<typeof tuple>;
((_x: T) => {})([
{},
]);
}
{
const the_ref = s.ref<t.NumberType>('#/hello');
type T = t.TSType<typeof the_ref>;
((_X: T) => {})(1);
}
{
const string = s.string({});
const tuple = s.tuple({'items': s.items(s.string(), s.integer())});
const any_of = s.anyOf(tuple, string);
const one_of = s.oneOf(string, tuple);
const all_of = s.allOf(string, tuple);
type T1 = t.TSType<typeof any_of>;
type T2 = t.TSType<typeof one_of>;
type T3 = t.TSType<typeof all_of>;
meta.equal<T1, string | number>(true);
meta.equal<T2, string | number>(true);
meta.equal<T3, string | number>(true);
}
{
const refObject = s.object({
'properties': {
'ref_object': s.array({'items': s.number()}),
}
});
const complex_object = s.object({
'definitions': {
'hello': refObject,
},
'properties': {
'string': s.string(),
'number': s.number(),
'integer': s.integer(),
'boolean': s.boolean(),
// FIXME: s.object() does not work
'object': s.object({'properties': {}}),
'nested_object': s.object({
'properties': {
// FIXME: s.array() does not work
'array': s.array({'items': true}),
}
}),
'array': s.array({'items': s.string()}),
'tuple': s.tuple({'items': s.items(s.string(), s.number(), s.object({'properties': {'hello': s.string()}}))}),
'true': true,
// 'false': false,
'ref': s.ref<typeof refObject>('hello'),
},
});
type T = t.TSType<typeof complex_object>;
((_x: T) => {})({
'string': '1',
'number': 1,
'integer': 1,
'boolean': true,
'object': {},
'nested_object': {
'array': [],
},
'array': [],
'tuple': ['hello', 1, {'hello': 'string'}],
'true': ['hello', 1, true, false],
// 'false': false,
'ref': {
'ref_object': [1, 2],
}
});
}
{
const sex = s.string<'male' | 'female'>({'enum': ['male', 'female']});
const personSchema = s.object({
'title': 'person',
'description': 'Person information',
'definitions': {
'sex': sex,
},
'properties': {
'name': s.string(),
'fullName': s.object({'properties': {
'firstName': s.string(),
'lastName': s.string(),
}}),
'age': s.number(),
'friends': s.array({'items': s.string()}),
'sex': s.ref<typeof sex>('#/definitions/sex'),
'location': s.oneOf(
s.string(),
s.tuple({'items': s.items(s.number(), s.number())}),
)
}
});
type IPerson = t.TSType<typeof personSchema>;
// IPerson is equivalent to
interface IPerson2 {
name: string;
fullName: {
firstName: string,
lastName: string,
};
age: number;
friends: string[];
sex: 'male' | 'female';
location: string | [number, number];
}
meta.equal<IPerson, IPerson2>(true);
}