Skip to content

Commit

Permalink
feat(combineLatest): add N-args signature for observable inputs (#5488)
Browse files Browse the repository at this point in the history
* feat(combineLatest): add N-args signature for observable inputs

When using combineLatest with an array of more than six
observable inputs, the result observable has a union type
of all observed types. Now, by using the 'as const' assertion,
the output observable has a correct tuple type that mirrors the
input observables.

* Remove trailing whitespace
  • Loading branch information
ggradnig authored Sep 2, 2020
1 parent c088b0e commit fcc47e7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
4 changes: 4 additions & 0 deletions spec-dtslint/observables/combineLatest-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ it('should have basic support for 7 or more params', () => {
const o = combineLatest([a$, b$, c$, d$, e$, f$, g$]); // $ExpectType Observable<(A | B | C | D | E | F | G)[]>
});

it('should have full support for 7 or more params with readonly tuples', () => {
const o = combineLatest([a$, b$, c$, d$, e$, f$, g$] as const); // $ExpectType Observable<readonly [A, B, C, D, E, F, G]>
});

it('should handle an array of Observables', () => {
const o = combineLatest([a$, a$, a$, a$, a$, a$, a$, a$, a$, a$, a$]); // $ExpectType Observable<A[]>
});
Expand Down
3 changes: 2 additions & 1 deletion src/internal/observable/combineLatest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Observable } from '../Observable';
import { ObservableInput, SchedulerLike, ObservedValueOf } from '../types';
import { ObservableInput, SchedulerLike, ObservedValueOf, ObservedValueTupleFromArray } from '../types';
import { isScheduler } from '../util/isScheduler';
import { isArray } from '../util/isArray';
import { Subscriber } from '../Subscriber';
Expand Down Expand Up @@ -67,6 +67,7 @@ export function combineLatest<O1 extends ObservableInput<any>, O2 extends Observ
export function combineLatest<O1 extends ObservableInput<any>, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>, O4 extends ObservableInput<any>, O5 extends ObservableInput<any>>(sources: [O1, O2, O3, O4, O5]): Observable<[ObservedValueOf<O1>, ObservedValueOf<O2>, ObservedValueOf<O3>, ObservedValueOf<O4>, ObservedValueOf<O5>]>;
export function combineLatest<O1 extends ObservableInput<any>, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>, O4 extends ObservableInput<any>, O5 extends ObservableInput<any>, O6 extends ObservableInput<any>>(sources: [O1, O2, O3, O4, O5, O6]): Observable<[ObservedValueOf<O1>, ObservedValueOf<O2>, ObservedValueOf<O3>, ObservedValueOf<O4>, ObservedValueOf<O5>, ObservedValueOf<O6>]>;
export function combineLatest<O extends ObservableInput<any>>(sources: O[]): Observable<ObservedValueOf<O>[]>;
export function combineLatest<O extends readonly ObservableInput<any>[]>(sources: O): Observable<ObservedValueTupleFromArray<O>>;

// Standard calls
/** @deprecated Pass arguments in a single array instead `combineLatest([a, b, c])` */
Expand Down
4 changes: 2 additions & 2 deletions src/internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export type ObservedValuesFromArray<X> = ObservedValueUnionFromArray<X>;
* of `[string, number]`.
*/
export type ObservedValueTupleFromArray<X> =
X extends Array<ObservableInput<any>>
X extends readonly ObservableInput<any>[]
? { [K in keyof X]: ObservedValueOf<X[K]> }
: never;

Expand Down Expand Up @@ -246,4 +246,4 @@ export type ValueFromNotification<T> = T extends { kind: 'N'|'E'|'C' } ?
(T extends NextNotification<any> ?
(T extends { value: infer V } ? V : undefined )
: never)
: never;
: never;

0 comments on commit fcc47e7

Please sign in to comment.