Skip to content

Commit

Permalink
fix: combineLatestWith and zipWith infer types from n-arguments (#5257)
Browse files Browse the repository at this point in the history
* refactor: use renamed ObservedValueUnionFromArray

* fix: use non-union n-args combineLatestWith sig

* fix: use non-union n-args zipWith sig

* chore: kick CI ... again
  • Loading branch information
cartant authored and benlesh committed Jan 27, 2020
1 parent ef804f9 commit 3e282a5
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 26 deletions.
2 changes: 1 addition & 1 deletion spec-dtslint/operators/combineLatestWith-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('combineLatestWith', () => {
const e = of('j', 'k', 'l');
const f = of('m', 'n', 'o');
const g = of('p', 'q', 'r');
const res = a.pipe(combineLatestWith(b, c, d, e, f, g)); // $ExpectType Observable<(string | number)[]>
const res = a.pipe(combineLatestWith(b, c, d, e, f, g)); // $ExpectType Observable<[number, string, string, string, string, string, string]>
});
});
});
57 changes: 57 additions & 0 deletions spec-dtslint/operators/zipWith-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { of } from 'rxjs';
import { zipWith } from 'rxjs/operators';

describe('zipWith', () => {
describe('without project parameter', () => {
it('should infer correctly with 1 param', () => {
const a = of(1, 2, 3);
const b = of('a', 'b', 'c');
const res = a.pipe(zipWith(b)); // $ExpectType Observable<[number, string]>
});

it('should infer correctly with 2 params', () => {
const a = of(1, 2, 3);
const b = of('a', 'b', 'c');
const c = of('d', 'e', 'f');
const res = a.pipe(zipWith(b, c)); // $ExpectType Observable<[number, string, string]>
});

it('should infer correctly with 3 params', () => {
const a = of(1, 2, 3);
const b = of('a', 'b', 'c');
const c = of('d', 'e', 'f');
const d = of('g', 'h', 'i');
const res = a.pipe(zipWith(b, c, d)); // $ExpectType Observable<[number, string, string, string]>
});

it('should infer correctly with 4 params', () => {
const a = of(1, 2, 3);
const b = of('a', 'b', 'c');
const c = of('d', 'e', 'f');
const d = of('g', 'h', 'i');
const e = of('j', 'k', 'l');
const res = a.pipe(zipWith(b, c, d, e)); // $ExpectType Observable<[number, string, string, string, string]>
});

it('should infer correctly with 5 params', () => {
const a = of(1, 2, 3);
const b = of('a', 'b', 'c');
const c = of('d', 'e', 'f');
const d = of('g', 'h', 'i');
const e = of('j', 'k', 'l');
const f = of('m', 'n', 'o');
const res = a.pipe(zipWith(b, c, d, e, f)); // $ExpectType Observable<[number, string, string, string, string, string]>
});

it('should accept N params', () => {
const a = of(1, 2, 3);
const b = of('a', 'b', 'c');
const c = of('d', 'e', 'f');
const d = of('g', 'h', 'i');
const e = of('j', 'k', 'l');
const f = of('m', 'n', 'o');
const g = of('p', 'q', 'r');
const res = a.pipe(zipWith(b, c, d, e, f, g)); // $ExpectType Observable<[number, string, string, string, string, string, string]>
});
});
});
14 changes: 2 additions & 12 deletions src/internal/operators/combineLatestWith.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isArray } from '../util/isArray';
import { CombineLatestOperator } from '../observable/combineLatest';
import { from } from '../observable/from';
import { Observable } from '../Observable';
import { ObservableInput, OperatorFunction, ObservedValuesFromArray } from '../types';
import { ObservableInput, OperatorFunction, ObservedValueUnionFromArray, ObservedValueTupleFromArray, Unshift } from '../types';

/* tslint:disable:max-line-length */
/** @deprecated use {@link combineLatestWith} */
Expand Down Expand Up @@ -58,16 +58,6 @@ export function combineLatest<T, R>(...observables: Array<ObservableInput<any> |
new CombineLatestOperator(project)
) as Observable<R>;
}
/* tslint:disable:max-line-length */
export function combineLatestWith<T, T2>(v2: ObservableInput<T2>): OperatorFunction<T, [T, T2]>;
export function combineLatestWith<T, T2, T3>(v2: ObservableInput<T2>, v3: ObservableInput<T3>): OperatorFunction<T, [T, T2, T3]>;
export function combineLatestWith<T, T2, T3, T4>(v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>): OperatorFunction<T, [T, T2, T3, T4]>;
export function combineLatestWith<T, T2, T3, T4, T5>(v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>): OperatorFunction<T, [T, T2, T3, T4, T5]>;
export function combineLatestWith<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]> ;
export function combineLatestWith<T, A extends ObservableInput<any>[]>(
...otherSources: A
): OperatorFunction<T, Array<T | ObservedValuesFromArray<A>>>;
/* tslint:enable:max-line-length */

/**
* Create an observable that combines the latest values from all passed observables and the source
Expand Down Expand Up @@ -107,6 +97,6 @@ export function combineLatestWith<T, A extends ObservableInput<any>[]>(
*/
export function combineLatestWith<T, A extends ObservableInput<any>[]>(
...otherSources: A
): OperatorFunction<T, Array<T | ObservedValuesFromArray<A>>> {
): OperatorFunction<T, Unshift<ObservedValueTupleFromArray<A>, T>> {
return combineLatest(...otherSources);
}
6 changes: 3 additions & 3 deletions src/internal/operators/mergeWith.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { merge as mergeStatic } from '../observable/merge';
import { Observable } from '../Observable';
import { ObservableInput, OperatorFunction, MonoTypeOperatorFunction, SchedulerLike, ObservedValuesFromArray } from '../types';
import { ObservableInput, OperatorFunction, MonoTypeOperatorFunction, SchedulerLike, ObservedValueUnionFromArray } from '../types';

/* tslint:disable:max-line-length */

Expand Down Expand Up @@ -64,7 +64,7 @@ export function merge<T, R>(...observables: Array<ObservableInput<any> | Schedul
}

export function mergeWith<T>(): OperatorFunction<T, T>;
export function mergeWith<T, A extends ObservableInput<any>[]>(...otherSources: A): OperatorFunction<T, (T | ObservedValuesFromArray<A>)>;
export function mergeWith<T, A extends ObservableInput<any>[]>(...otherSources: A): OperatorFunction<T, (T | ObservedValueUnionFromArray<A>)>;

/**
* Merge the values from all observables to an single observable result.
Expand Down Expand Up @@ -105,6 +105,6 @@ export function mergeWith<T, A extends ObservableInput<any>[]>(...otherSources:
* ```
* @param otherSources the sources to combine the current source with.
*/
export function mergeWith<T, A extends ObservableInput<any>[]>(...otherSources: A): OperatorFunction<T, (T | ObservedValuesFromArray<A>)> {
export function mergeWith<T, A extends ObservableInput<any>[]>(...otherSources: A): OperatorFunction<T, (T | ObservedValueUnionFromArray<A>)> {
return merge(...otherSources);
}
14 changes: 4 additions & 10 deletions src/internal/operators/zipWith.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { zip as zipStatic } from '../observable/zip';
import { Observable } from '../Observable';
import { ObservableInput, OperatorFunction, ObservedValuesFromArray } from '../types';
import { ObservableInput, OperatorFunction, ObservedValueTupleFromArray, Unshift } from '../types';

/* tslint:disable:max-line-length */
/** @deprecated Deprecated use {@link zipWith} */
Expand Down Expand Up @@ -45,14 +45,6 @@ export function zip<T, R>(...observables: Array<ObservableInput<any> | ((...valu
};
}

/* tslint:disable:max-line-length */
export function zipWith<T, T2>(v2: ObservableInput<T2>): OperatorFunction<T, [T, T2]>;
export function zipWith<T, T2, T3>(v2: ObservableInput<T2>, v3: ObservableInput<T3>): OperatorFunction<T, [T, T2, T3]>;
export function zipWith<T, T2, T3, T4>(v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>): OperatorFunction<T, [T, T2, T3, T4]>;
export function zipWith<T, T2, T3, T4, T5>(v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>): OperatorFunction<T, [T, T2, T3, T4, T5]>;
export function zipWith<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]> ;
export function zipWith<T, A extends ObservableInput<any>[]>(...otherInputs: A): OperatorFunction<T, Array<T | ObservedValuesFromArray<A>>>;
/* tslint:enable:max-line-length */
/**
* Subscribes to the source, and the observable inputs provided as arguments, and combines their values, by index, into arrays.
*
Expand All @@ -73,6 +65,8 @@ export function zipWith<T, A extends ObservableInput<any>[]>(...otherInputs: A):
*
* @param otherInputs other observable inputs to collate values from.
*/
export function zipWith<T, A extends ObservableInput<any>[]>(...otherInputs: A): OperatorFunction<T, Array<T | ObservedValuesFromArray<A>>> {
export function zipWith<T, A extends ObservableInput<any>[]>(
...otherInputs: A
): OperatorFunction<T, Unshift<ObservedValueTupleFromArray<A>, T>> {
return zip(...otherInputs);
}

0 comments on commit 3e282a5

Please sign in to comment.