forked from ReactiveX/rxjs
-
Notifications
You must be signed in to change notification settings - Fork 4
/
pipe-spec.ts
129 lines (101 loc) · 5.11 KB
/
pipe-spec.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 { pipe, UnaryFunction, of, Observable } from 'rxjs';
/**
* Used to keep the tests uncluttered.
*
* Returns a `UnaryFunction` with the
* specified literal type parameters.
* That is, `a('0', '1')` returns `UnaryFunction<'0', '1'>`.
* That means that the `a` function can be used to create consecutive
* arguments that are either compatible or incompatible.
*
* ```js
* a('0', '1'), a('1', '2') // OK
* a('0', '1'), a('#', '2') // Error '1' is not compatible with '#'
* ```
*
* @param {string} input The `UnaryFunction` input type parameter
* @param {string} output The `UnaryFunction` output type parameter
*/
function a<I extends string, O extends string>(input: I, output: O): UnaryFunction<I, O> {
return i => output;
}
it('should infer unknown for no arguments', () => {
const o = pipe(); // $ExpectType <T>(x: T) => T
});
it('should infer for 1 argument', () => {
const o = pipe(a('0', '1')); // $ExpectType UnaryFunction<"0", "1">
});
it('should infer for 2 arguments', () => {
const o = pipe(a('0', '1'), a('1', '2')); // $ExpectType UnaryFunction<"0", "2">
});
it('should infer for 3 arguments', () => {
const o = pipe(a('0', '1'), a('1', '2'), a('2', '3')); // $ExpectType UnaryFunction<"0", "3">
});
it('should infer for 4 arguments', () => {
const o = pipe(a('0', '1'), a('1', '2'), a('2', '3'), a('3', '4')); // $ExpectType UnaryFunction<"0", "4">
});
it('should infer for 5 arguments', () => {
const o = pipe(a('0', '1'), a('1', '2'), a('2', '3'), a('3', '4'), a('4', '5')); // $ExpectType UnaryFunction<"0", "5">
});
it('should infer for 6 arguments', () => {
const o = pipe(a('0', '1'), a('1', '2'), a('2', '3'), a('3', '4'), a('4', '5'), a('5', '6')); // $ExpectType UnaryFunction<"0", "6">
});
it('should infer for 7 arguments', () => {
const o = pipe(a('0', '1'), a('1', '2'), a('2', '3'), a('3', '4'), a('4', '5'), a('5', '6'), a('6', '7')); // $ExpectType UnaryFunction<"0", "7">
});
it('should infer for 8 arguments', () => {
const o = pipe(a('0', '1'), a('1', '2'), a('2', '3'), a('3', '4'), a('4', '5'), a('5', '6'), a('6', '7'), a('7', '8')); // $ExpectType UnaryFunction<"0", "8">
});
it('should infer for 9 arguments', () => {
const o = pipe(a('0', '1'), a('1', '2'), a('2', '3'), a('3', '4'), a('4', '5'), a('5', '6'), a('6', '7'), a('7', '8'), a('8', '9')); // $ExpectType UnaryFunction<"0", "9">
});
it('should infer unknown for more than 9 arguments', () => {
const o = pipe(a('0', '1'), a('1', '2'), a('2', '3'), a('3', '4'), a('4', '5'), a('5', '6'), a('6', '7'), a('7', '8'), a('8', '9'), a('9', '10')); // $ExpectType UnaryFunction<"0", unknown>
});
it('should require a type assertion for more than 9 arguments', () => {
const o: UnaryFunction<'0', '10'> = pipe(a('0', '1'), a('1', '2'), a('2', '3'), a('3', '4'), a('4', '5'), a('5', '6'), a('6', '7'), a('7', '8'), a('8', '9'), a('9', '10')); // $ExpectError
});
it('should enforce types for the 2nd argument', () => {
const o = pipe(a('0', '1'), a('#', '2')); // $ExpectError
});
it('should enforce types for the 3rd argument', () => {
const o = pipe(a('0', '1'), a('1', '2'), a('#', '3')); // $ExpectError
});
it('should enforce types for the 4th argument', () => {
const o = pipe(a('0', '1'), a('1', '2'), a('2', '3'), a('#', '4')); // $ExpectError
});
it('should enforce types for the 5th argument', () => {
const o = pipe(a('0', '1'), a('1', '2'), a('2', '3'), a('3', '4'), a('#', '5')); // $ExpectError
});
it('should enforce types for the 6th argument', () => {
const o = pipe(a('0', '1'), a('1', '2'), a('2', '3'), a('3', '4'), a('4', '5'), a('#', '6')); // $ExpectError
});
it('should enforce types for the 7th argument', () => {
const o = pipe(a('0', '1'), a('1', '2'), a('2', '3'), a('3', '4'), a('4', '5'), a('5', '6'), a('#', '7')); // $ExpectError
});
it('should enforce types for the 8th argument', () => {
const o = pipe(a('0', '1'), a('1', '2'), a('2', '3'), a('3', '4'), a('4', '5'), a('5', '6'), a('6', '7'), a('#', '8')); // $ExpectError
});
it('should enforce types for the 9th argument', () => {
const o = pipe(a('0', '1'), a('1', '2'), a('2', '3'), a('3', '4'), a('4', '5'), a('5', '6'), a('6', '7'), a('7', '8'), a('#', '9')); // $ExpectError
});
it('should return a non-narrowed Observable type', () => {
const customOperator = <T>(p: T) => (a: Observable<T>) => a;
const staticPipe = pipe(customOperator('infer'));
const o = of('foo').pipe(staticPipe); // $ExpectType Observable<string>
});
it('should return an explicit Observable type', () => {
const customOperator = <T>() => (a: Observable<T>) => a;
const staticPipe = pipe(customOperator<string>());
const o = of('foo').pipe(staticPipe); // $ExpectType Observable<string>
});
it('should return Observable<unknown> when T cannot be inferred', () => {
const customOperator = <T>() => (a: Observable<T>) => a;
// type can't be possibly be inferred here
const staticPipe = pipe(customOperator());
const o = of('foo').pipe(staticPipe); // $ExpectType Observable<unknown>
});
it('should return a non-narrowed type', () => {
const func = pipe((value: string) => value, (value: string) => value + value);
const value = func('foo'); // $ExpectType string
});