Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typescript): Update typings for race to support proper return types #4465

Merged
merged 2 commits into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions spec-dtslint/observables/race-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,51 @@ import { race, of } from 'rxjs';

it('should infer correctly with 1 parameter', () => {
const a = of(1);
const res = race(a); // $ExpectType Observable<number>
const o = race(a); // $ExpectType Observable<number>
});

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<number>
const o = race(a, b); // $ExpectType Observable<number>
});

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<string> | Observable<number>
});

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<string> | Observable<number> | Observable<boolean>
});

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<string> | Observable<number> | Observable<boolean> | Observable<number[]>
});

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<string> | Observable<number> | Observable<boolean> | Observable<number[]> | Observable<string[]>
});

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<{}>
});
15 changes: 12 additions & 3 deletions src/internal/observable/race.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@ import { OuterSubscriber } from '../OuterSubscriber';
import { InnerSubscriber } from '../InnerSubscriber';
import { subscribeToResult } from '../util/subscribeToResult';

// tslint:disable:max-line-length
export function race<A, B>(a: Observable<A>, b: Observable<B>): Observable<A> | Observable<B>;
export function race<A, B, C>(a: Observable<A>, b: Observable<B>, c: Observable<C>): Observable<A> | Observable<B> | Observable<C>;
export function race<A, B, C, D>(a: Observable<A>, b: Observable<B>, c: Observable<C>, d: Observable<D>): Observable<A> | Observable<B> | Observable<C> | Observable<D>;
export function race<A, B, C, D, E>(a: Observable<A>, b: Observable<B>, c: Observable<C>, d: Observable<D>, e: Observable<E>): Observable<A> | Observable<B> | Observable<C> | Observable<D> | Observable<E>;
// tslint:enable:max-line-length

export function race<T>(observables: Array<Observable<T>>): Observable<T>;
export function race<T>(observables: Array<Observable<any>>): Observable<T>;
export function race<T>(...observables: Array<Observable<T> | Array<Observable<T>>>): Observable<T>;
Copy link
Collaborator

@cartant cartant Jan 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why

export function race<T>(...observables: Array<Observable<T> | Array<Observable<T>>>): Observable<T>;

needs to be an overload signature. I can see why it is the implementation signature, but why it needs to be an overload baffles me. Don't the others handle the scenarios that are bundled into this signature?

Do we really need to keep this already-existing signature? Can it be removed?

LGTM, otherwise.

export function race(...observables: Observable<any>[]): Observable<{}>;

/**
* Returns an Observable that mirrors the first source Observable to emit an item.
*
Expand All @@ -38,14 +47,14 @@ export function race<T>(...observables: Array<Observable<T> | Array<Observable<T
* @name race
* @owner Observable
*/
export function race<T>(...observables: Array<Observable<any> | Array<Observable<any>>>): Observable<T> {
export function race<T>(...observables: (Observable<any>[] | Observable<any>)[]): Observable<T> {
// 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 = <Array<Observable<any>>>observables[0];
observables = observables[0] as Observable<any>[];
} else {
return <Observable<any>>observables[0];
return observables[0] as Observable<T>;
}
}

Expand Down