From 0042846c13aee35b1da55672d8150651f75dfc49 Mon Sep 17 00:00:00 2001 From: Ben Lesh Date: Mon, 14 Jan 2019 11:47:29 -0800 Subject: [PATCH] fix(typescript): Update typings for race to support proper return types (#4465) * fix(typescript): Update typings for race to support proper return types * fixup! fix(typescript): Update typings for race to support proper return types --- spec-dtslint/observables/race-spec.ts | 59 +++++++++++++++++++++++++-- src/internal/observable/race.ts | 21 +++++++--- src/internal/operators/race.ts | 6 +-- 3 files changed, 73 insertions(+), 13 deletions(-) diff --git a/spec-dtslint/observables/race-spec.ts b/spec-dtslint/observables/race-spec.ts index c00f3e08cf..805d8a6137 100644 --- a/spec-dtslint/observables/race-spec.ts +++ b/spec-dtslint/observables/race-spec.ts @@ -2,17 +2,68 @@ import { race, of } from 'rxjs'; it('should infer correctly with 1 parameter', () => { const a = of(1); - const res = race(a); // $ExpectType Observable + const o = race(a); // $ExpectType Observable }); it('should infer correctly with multiple parameters of the same type', () => { const a = of(1); const b = of(2); - const res = race(a, b); // $ExpectType Observable + const o = race(a, b); // $ExpectType Observable }); -it('should not support multiple parameters with different type', () => { +it('should support 2 parameters with different types', () => { const a = of(1); const b = of('a'); - const res = race(a, b); // $ExpectError + const o = race(a, b); // $ExpectType Observable | Observable +}); + +it('should support 3 parameters with different types', () => { + const a = of(1); + const b = of('a'); + const c = of(true); + const o = race(a, b, c); // $ExpectType Observable | Observable | Observable +}); + +it('should support 4 parameters with different types', () => { + const a = of(1); + const b = of('a'); + const c = of(true); + const d = of([1, 2, 3]); + const o = race(a, b, c, d); // $ExpectType Observable | Observable | Observable | Observable +}); + +it('should support 5 parameters with different types', () => { + const a = of(1); + const b = of('a'); + const c = of(true); + const d = of([1, 2, 3]); + const e = of(['blah']); + const o = race(a, b, c, d, e); // $ExpectType Observable | Observable | Observable | Observable | Observable +}); + +it('should support 6 or more parameters of the same type', () => { + const a = of(1); + const o = race(a, a, a, a, a, a, a, a, a, a, a, a, a, a); // $ExpectType Observable +}); + +it('should return {} for 6 or more arguments of different types', () => { + const a = of(1); + const b = of('a'); + const c = of(true); + const d = of([1, 2, 3]); + const e = of(['blah']); + const f = of({ foo: 'bar' }); + const o = race(a, b, c, d, e, f); // $ExpectType Observable<{}> +}); + +it('should handle an array of observables', () => { + const a = of(1); + const b = of(2); + const o = race([a, b]); // $ExpectType Observable +}); + +it('should return {} for array of observables of different types', () => { + const a = of(1); + const b = of('test'); + const o = race([a, b]); // $ExpectType Observable<{}> }); diff --git a/src/internal/observable/race.ts b/src/internal/observable/race.ts index 0f0d24c919..014734ddaf 100644 --- a/src/internal/observable/race.ts +++ b/src/internal/observable/race.ts @@ -9,9 +9,18 @@ import { OuterSubscriber } from '../OuterSubscriber'; import { InnerSubscriber } from '../InnerSubscriber'; import { subscribeToResult } from '../util/subscribeToResult'; -export function race(observables: Array>): Observable; -export function race(observables: Array>): Observable; -export function race(...observables: Array | Array>>): Observable; +// tslint:disable:max-line-length +export function race(a: Observable, b: Observable): Observable | Observable; +export function race(a: Observable, b: Observable, c: Observable): Observable | Observable | Observable; +export function race(a: Observable, b: Observable, c: Observable, d: Observable): Observable | Observable | Observable | Observable; +export function race(a: Observable, b: Observable, c: Observable, d: Observable, e: Observable): Observable | Observable | Observable | Observable | Observable; +// tslint:enable:max-line-length + +export function race(observables: Observable[]): Observable; +export function race(observables: Observable[]): Observable<{}>; +export function race(...observables: Observable[]): Observable; +export function race(...observables: Observable[]): Observable<{}>; + /** * Returns an Observable that mirrors the first source Observable to emit an item. * @@ -38,14 +47,14 @@ export function race(...observables: Array | Array(...observables: Array | Array>>): Observable { +export function race(...observables: (Observable[] | Observable)[]): Observable { // if the only argument is an array, it was most likely called with // `race([obs1, obs2, ...])` if (observables.length === 1) { if (isArray(observables[0])) { - observables = >>observables[0]; + observables = observables[0] as Observable[]; } else { - return >observables[0]; + return observables[0] as Observable; } } diff --git a/src/internal/operators/race.ts b/src/internal/operators/race.ts index 6e914100bf..5cefbf0c3f 100644 --- a/src/internal/operators/race.ts +++ b/src/internal/operators/race.ts @@ -23,14 +23,14 @@ export function race(...observables: Array | Array(...observables: Array | Array>>): MonoTypeOperatorFunction { +export function race(...observables: (Observable | Observable[])[]): MonoTypeOperatorFunction { return function raceOperatorFunction(source: Observable) { // if the only argument is an array, it was most likely called with // `pair([obs1, obs2, ...])` if (observables.length === 1 && isArray(observables[0])) { - observables = >>observables[0]; + observables = observables[0] as Observable[]; } - return source.lift.call(raceStatic(source, ...observables)); + return source.lift.call(raceStatic(source, ...(observables as Observable[]))); }; }