Skip to content

Commit 1e19a24

Browse files
committed
fix(types): support passing union types to withLatestFrom
1 parent 0d87f52 commit 1e19a24

File tree

2 files changed

+116
-11
lines changed

2 files changed

+116
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { of } from 'rxjs';
2+
import { withLatestFrom } from 'rxjs/operators';
3+
4+
describe('withLatestFrom', () => {
5+
describe('without project parameter', () => {
6+
it('should infer correctly with 1 param', () => {
7+
const a = of(1, 2, 3);
8+
const b = of('a', 'b', 'c');
9+
const res = a.pipe(withLatestFrom(b)); // $ExpectType Observable<[number, string]>
10+
});
11+
12+
it('should infer correctly with 2 params', () => {
13+
const a = of(1, 2, 3);
14+
const b = of('a', 'b', 'c');
15+
const c = of('d', 'e', 'f');
16+
const res = a.pipe(withLatestFrom(b, c)); // $ExpectType Observable<[number, string, string]>
17+
});
18+
19+
it('should infer correctly with 3 params', () => {
20+
const a = of(1, 2, 3);
21+
const b = of('a', 'b', 'c');
22+
const c = of('d', 'e', 'f');
23+
const d = of('g', 'h', 'i');
24+
const res = a.pipe(withLatestFrom(b, c, d)); // $ExpectType Observable<[number, string, string, string]>
25+
});
26+
27+
it('should infer correctly with 4 params', () => {
28+
const a = of(1, 2, 3);
29+
const b = of('a', 'b', 'c');
30+
const c = of('d', 'e', 'f');
31+
const d = of('g', 'h', 'i');
32+
const e = of('j', 'k', 'l');
33+
const res = a.pipe(withLatestFrom(b, c, d, e)); // $ExpectType Observable<[number, string, string, string, string]>
34+
});
35+
36+
it('should infer correctly with 5 params', () => {
37+
const a = of(1, 2, 3);
38+
const b = of('a', 'b', 'c');
39+
const c = of('d', 'e', 'f');
40+
const d = of('g', 'h', 'i');
41+
const e = of('j', 'k', 'l');
42+
const f = of('m', 'n', 'o');
43+
const res = a.pipe(withLatestFrom(b, c, d, e, f)); // $ExpectType Observable<[number, string, string, string, string, string]>
44+
});
45+
46+
it('should only accept maximum params of 5', () => {
47+
const a = of(1, 2, 3);
48+
const b = of('a', 'b', 'c');
49+
const c = of('d', 'e', 'f');
50+
const d = of('g', 'h', 'i');
51+
const e = of('j', 'k', 'l');
52+
const f = of('m', 'n', 'o');
53+
const g = of('p', 'q', 'r');
54+
const res = a.pipe(withLatestFrom(b, c, d, e, f, g)); // $ExpectType Observable<{}>
55+
});
56+
});
57+
58+
describe('with project parameter', () => {
59+
it('should infer correctly with project param', () => {
60+
const a = of(1, 2, 3);
61+
const res = a.pipe(withLatestFrom(v1 => 'b')); // $ExpectType Observable<string>
62+
});
63+
64+
it('should infer correctly with 1 param', () => {
65+
const a = of(1, 2, 3);
66+
const b = of('a', 'b', 'c');
67+
const res = a.pipe(withLatestFrom(b, (a, b) => b)); // $ExpectType Observable<string>
68+
});
69+
70+
it('should infer correctly with 2 params', () => {
71+
const a = of(1, 2, 3);
72+
const b = of('a', 'b', 'c');
73+
const c = of('d', 'e', 'f');
74+
const res = a.pipe(withLatestFrom(b, c, (a, b, c) => b + c)); // $ExpectType Observable<string>
75+
});
76+
77+
it('should infer correctly with 3 params', () => {
78+
const a = of(1, 2, 3);
79+
const b = of('a', 'b', 'c');
80+
const c = of('d', 'e', 'f');
81+
const d = of('g', 'h', 'i');
82+
const ref = a.pipe(withLatestFrom(b, c, d, (a, b, c, d) => b + c)); // $ExpectType Observable<string>
83+
});
84+
85+
it('should infer correctly with 4 params', () => {
86+
const a = of(1, 2, 3);
87+
const b = of('a', 'b', 'c');
88+
const c = of('d', 'e', 'f');
89+
const d = of('g', 'h', 'i');
90+
const e = of('j', 'k', 'l');
91+
const res = a.pipe(withLatestFrom(b, c, d, e, (a, b, c, d, e) => b + c)); // $ExpectType Observable<string>
92+
});
93+
94+
it('should infer correctly with 5 params', () => {
95+
const a = of(1, 2, 3);
96+
const b = of('a', 'b', 'c');
97+
const c = of('d', 'e', 'f');
98+
const d = of('g', 'h', 'i');
99+
const e = of('j', 'k', 'l');
100+
const f = of('m', 'n', 'o');
101+
const res = a.pipe(withLatestFrom(b, c, d, e, f, (a, b, c, d, e, f) => b + c)); // $ExpectType Observable<string>
102+
});
103+
});
104+
});

src/internal/operators/withLatestFrom.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@ import { Observable } from '../Observable';
44
import { OuterSubscriber } from '../OuterSubscriber';
55
import { InnerSubscriber } from '../InnerSubscriber';
66
import { subscribeToResult } from '../util/subscribeToResult';
7-
import { ObservableInput, OperatorFunction } from '../types';
7+
import { ObservableInput, OperatorFunction, ObservedValueOf } from '../types';
88

99
/* tslint:disable:max-line-length */
1010
export function withLatestFrom<T, R>(project: (v1: T) => R): OperatorFunction<T, R>;
11-
export function withLatestFrom<T, T2, R>(v2: ObservableInput<T2>, project: (v1: T, v2: T2) => R): OperatorFunction<T, R>;
12-
export function withLatestFrom<T, T2, T3, R>(v2: ObservableInput<T2>, v3: ObservableInput<T3>, project: (v1: T, v2: T2, v3: T3) => R): OperatorFunction<T, R>;
13-
export function withLatestFrom<T, T2, T3, T4, R>(v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, project: (v1: T, v2: T2, v3: T3, v4: T4) => R): OperatorFunction<T, R>;
14-
export function withLatestFrom<T, T2, T3, T4, T5, R>(v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, project: (v1: T, v2: T2, v3: T3, v4: T4, v5: T5) => R): OperatorFunction<T, R>;
15-
export function withLatestFrom<T, T2, T3, T4, T5, T6, R>(v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, v6: ObservableInput<T6>, project: (v1: T, v2: T2, v3: T3, v4: T4, v5: T5, v6: T6) => R): OperatorFunction<T, R> ;
16-
export function withLatestFrom<T, T2>(v2: ObservableInput<T2>): OperatorFunction<T, [T, T2]>;
17-
export function withLatestFrom<T, T2, T3>(v2: ObservableInput<T2>, v3: ObservableInput<T3>): OperatorFunction<T, [T, T2, T3]>;
18-
export function withLatestFrom<T, T2, T3, T4>(v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>): OperatorFunction<T, [T, T2, T3, T4]>;
19-
export function withLatestFrom<T, T2, T3, T4, T5>(v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>): OperatorFunction<T, [T, T2, T3, T4, T5]>;
20-
export function withLatestFrom<T, T2, T3, T4, T5, T6>(v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, v6: ObservableInput<T6>): OperatorFunction<T, [T, T2, T3, T4, T5, T6]> ;
11+
export function withLatestFrom<T, O2 extends ObservableInput<any>, R>(source2: O2, project: (v1: T, v2: ObservedValueOf<O2>) => R): OperatorFunction<T, R>;
12+
export function withLatestFrom<T, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>, R>(v2: O2, v3: O3, project: (v1: T, v2: ObservedValueOf<O2>, v3: ObservedValueOf<O3>) => R): OperatorFunction<T, R>;
13+
export function withLatestFrom<T, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>, O4 extends ObservableInput<any>, R>(v2: O2, v3: O3, v4: O4, project: (v1: T, v2: ObservedValueOf<O2>, v3: ObservedValueOf<O3>, v4: ObservedValueOf<O4>) => R): OperatorFunction<T, R>;
14+
export function withLatestFrom<T, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>, O4 extends ObservableInput<any>, O5 extends ObservableInput<any>, R>(v2: O2, v3: O3, v4: O4, v5: O5, project: (v1: T, v2: ObservedValueOf<O2>, v3: ObservedValueOf<O3>, v4: ObservedValueOf<O4>, v5: ObservedValueOf<O5>) => R): OperatorFunction<T, R>;
15+
export function withLatestFrom<T, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>, O4 extends ObservableInput<any>, O5 extends ObservableInput<any>, O6 extends ObservableInput<any>, R>(v2: O2, v3: O3, v4: O4, v5: O5, v6: O6, project: (v1: T, v2: ObservedValueOf<O2>, v3: ObservedValueOf<O3>, v4: ObservedValueOf<O4>, v5: ObservedValueOf<O5>, v6: ObservedValueOf<O6>) => R): OperatorFunction<T, R>;
16+
export function withLatestFrom<T, O2 extends ObservableInput<any>>(source2: O2): OperatorFunction<T, [T, ObservedValueOf<O2>]>;
17+
export function withLatestFrom<T, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>>(v2: O2, v3: O3): OperatorFunction<T, [T, ObservedValueOf<O2>, ObservedValueOf<O3>]>;
18+
export function withLatestFrom<T, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>, O4 extends ObservableInput<any>>(v2: O2, v3: O3, v4: O4): OperatorFunction<T, [T, ObservedValueOf<O2>, ObservedValueOf<O3>, ObservedValueOf<O4>]>;
19+
export function withLatestFrom<T, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>, O4 extends ObservableInput<any>, O5 extends ObservableInput<any>>(v2: O2, v3: O3, v4: O4, v5: O5): OperatorFunction<T, [T, ObservedValueOf<O2>, ObservedValueOf<O3>, ObservedValueOf<O4>, ObservedValueOf<O5>]>;
20+
export function withLatestFrom<T, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>, O4 extends ObservableInput<any>, O5 extends ObservableInput<any>, O6 extends ObservableInput<any>>(v2: O2, v3: O3, v4: O4, v5: O5, v6: O6): OperatorFunction<T, [T, ObservedValueOf<O2>, ObservedValueOf<O3>, ObservedValueOf<O4>, ObservedValueOf<O5>, ObservedValueOf<O6>]>;
2121
export function withLatestFrom<T, R>(...observables: Array<ObservableInput<any> | ((...values: Array<any>) => R)>): OperatorFunction<T, R>;
2222
export function withLatestFrom<T, R>(array: ObservableInput<any>[]): OperatorFunction<T, R>;
2323
export function withLatestFrom<T, R>(array: ObservableInput<any>[], project: (...values: Array<any>) => R): OperatorFunction<T, R>;
24+
2425
/* tslint:enable:max-line-length */
2526

2627
/**

0 commit comments

Comments
 (0)