Skip to content

Commit

Permalink
feat(combineLatest): add N-args signature for observable inputs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ggradnig committed Jun 13, 2020
1 parent 3f3c614 commit ab60906
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 @@ -165,7 +165,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 @@ -201,4 +201,4 @@ export type Tail<X extends any[]> =
* If you have `T extends Array<any>`, and pass a `string[]` to it,
* `ValueFromArray<T>` will return the actual type of `string`.
*/
export type ValueFromArray<A> = A extends Array<infer T> ? T : never;
export type ValueFromArray<A> = A extends Array<infer T> ? T : never;

0 comments on commit ab60906

Please sign in to comment.