-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-types.ts
129 lines (116 loc) · 4.1 KB
/
test-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
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
import { pIter, pIterEnumerated, pIterSettled } from './index.js';
import { expectTypeOf } from 'expect-type';
function* promiseGenerator() {
yield Promise.resolve(42);
yield Promise.resolve('foo');
yield 42;
yield 'foo';
}
async function* awaitedGenerator() {
yield 42;
yield 'foo';
}
// Test pIter()
// Yields an async generator over the awaited types of the input
expectTypeOf(pIter(promiseGenerator())).toEqualTypeOf<
AsyncGenerator<number | string, void>
>();
expectTypeOf(pIter(promiseGenerator())).toMatchTypeOf<
AsyncGenerator<number | string>
>();
expectTypeOf(pIter(promiseGenerator())).toMatchTypeOf<
AsyncIterable<number | string>
>();
expectTypeOf(pIter(promiseGenerator())).toMatchTypeOf<
AsyncIterator<number | string>
>();
// Equals the type of a standard async generator
expectTypeOf(pIter(promiseGenerator())).toEqualTypeOf(awaitedGenerator());
// Handles different combinations of input types
expectTypeOf(
pIter([Promise.resolve(42), Promise.resolve('foo')])
).toEqualTypeOf<AsyncGenerator<number | string, void>>();
expectTypeOf(pIter([Promise.resolve(42), 42])).toEqualTypeOf<
AsyncGenerator<number, void>
>();
expectTypeOf(pIter([Promise.resolve(42), 'foo'])).toEqualTypeOf<
AsyncGenerator<number | string, void>
>();
expectTypeOf(pIter([Promise.resolve(42), Promise.resolve(42)])).toEqualTypeOf<
AsyncGenerator<number, void>
>();
// Test pIterEnumerated
// Yields an async generator over pairs [number, awaited types of the input]
expectTypeOf(pIterEnumerated(promiseGenerator())).toEqualTypeOf<
AsyncGenerator<[number, number | string], void>
>();
expectTypeOf(pIterEnumerated(promiseGenerator())).toMatchTypeOf<
AsyncGenerator<[number, number | string]>
>();
expectTypeOf(pIterEnumerated(promiseGenerator())).toMatchTypeOf<
AsyncIterable<[number, number | string]>
>();
expectTypeOf(pIterEnumerated(promiseGenerator())).toMatchTypeOf<
AsyncIterator<[number, number | string]>
>();
// Handles different combinations of input types
expectTypeOf(
pIterEnumerated([Promise.resolve(42), Promise.resolve('foo')])
).toEqualTypeOf<AsyncGenerator<[number, number | string], void>>();
expectTypeOf(pIterEnumerated([Promise.resolve(42), 42])).toEqualTypeOf<
AsyncGenerator<[number, number], void>
>();
expectTypeOf(pIterEnumerated([Promise.resolve(42), 'foo'])).toEqualTypeOf<
AsyncGenerator<[number, number | string], void>
>();
expectTypeOf(
pIterEnumerated([Promise.resolve(42), Promise.resolve(42)])
).toEqualTypeOf<AsyncGenerator<[number, number], void>>();
// Test pIterSettled
// Yields an async generator over settled results
expectTypeOf(pIterSettled(promiseGenerator())).toEqualTypeOf<
AsyncGenerator<
PromiseSettledResult<number | string> & { index: number },
void
>
>();
expectTypeOf(pIterSettled(promiseGenerator())).toMatchTypeOf<
AsyncGenerator<PromiseSettledResult<number | string> & { index: number }>
>();
expectTypeOf(pIterSettled(promiseGenerator())).toMatchTypeOf<
AsyncIterable<PromiseSettledResult<number | string> & { index: number }>
>();
expectTypeOf(pIterSettled(promiseGenerator())).toMatchTypeOf<
AsyncIterator<PromiseSettledResult<number | string> & { index: number }>
>();
// Sanity-check the type of PromiseSettledResult
expectTypeOf(pIterSettled(promiseGenerator())).toEqualTypeOf<
AsyncGenerator<
| { status: 'fulfilled'; value: number | string; index: number }
| { status: 'rejected'; reason: any; index: number },
void
>
>();
// Handles different combinations of input types
expectTypeOf(
pIterSettled([Promise.resolve(42), Promise.resolve('foo')])
).toEqualTypeOf<
AsyncGenerator<
PromiseSettledResult<number | string> & { index: number },
void
>
>();
expectTypeOf(pIterSettled([Promise.resolve(42), 42])).toEqualTypeOf<
AsyncGenerator<PromiseSettledResult<number> & { index: number }, void>
>();
expectTypeOf(pIterSettled([Promise.resolve(42), 'foo'])).toEqualTypeOf<
AsyncGenerator<
PromiseSettledResult<number | string> & { index: number },
void
>
>();
expectTypeOf(
pIterSettled([Promise.resolve(42), Promise.resolve(42)])
).toEqualTypeOf<
AsyncGenerator<PromiseSettledResult<number> & { index: number }, void>
>();