Skip to content

Commit

Permalink
fix(reduce): type definitions overloads for TypeScript are now in pro…
Browse files Browse the repository at this point in the history
…per order (#2523)

* Change order of reduce overload type definitions

Returning type T takes precedence over returning type T[].

* test(reduce): add type tests for arrays

Add tests for reducing T[] to T[] (e.g. concatenating arrays into one array) and for reducing T to T[] (e.g. pushing items into an array).
  • Loading branch information
PrimalZed authored and benlesh committed Jun 14, 2017
1 parent a400cab commit ccc0647
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
27 changes: 27 additions & 0 deletions spec/operators/reduce-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,33 @@ describe('Observable.prototype.reduce', () => {
});
});

it('should accept T typed reducers when T is an array', () => {
type(() => {
let a: Rx.Observable<number[]>;
const reduced = a.reduce((acc, value) => {
return acc.concat(value);
}, []);

reduced.subscribe(rs => {
rs[0].toExponential();
});
});
});

it('should accept R typed reduces when R is an array of T', () => {
type(() => {
let a: Rx.Observable<number>;
const reduced = a.reduce((acc, value) => {
acc.push(value);
return acc;
}, []);

reduced.subscribe(rs => {
rs[0].toExponential();
});
});
});

it('should accept R typed reducers when R is assignable to T', () => {
type(() => {
let a: Rx.Observable<{ a?: number; b?: string }>;
Expand Down
2 changes: 1 addition & 1 deletion src/operator/reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Operator } from '../Operator';
import { Subscriber } from '../Subscriber';

/* tslint:disable:max-line-length */
export function reduce<T>(this: Observable<T>, accumulator: (acc: T[], value: T, index: number) => T[], seed: T[]): Observable<T[]>;
export function reduce<T>(this: Observable<T>, accumulator: (acc: T, value: T, index: number) => T, seed?: T): Observable<T>;
export function reduce<T>(this: Observable<T>, accumulator: (acc: T[], value: T, index: number) => T[], seed: T[]): Observable<T[]>;
export function reduce<T, R>(this: Observable<T>, accumulator: (acc: R, value: T, index: number) => R, seed: R): Observable<R>;
/* tslint:enable:max-line-length */

Expand Down

0 comments on commit ccc0647

Please sign in to comment.