diff --git a/spec/operators/reduce-spec.ts b/spec/operators/reduce-spec.ts index 1c228343d5..427342a77e 100644 --- a/spec/operators/reduce-spec.ts +++ b/spec/operators/reduce-spec.ts @@ -301,24 +301,85 @@ describe('Observable.prototype.reduce', () => { }); it('should accept T typed reducers', () => { + type(() => { + let a: Rx.Observable<{ a: number; b: string }>; + const reduced = a.reduce((acc, value) => { + value.a = acc.a; + value.b = acc.b; + return acc; + }); + + reduced.subscribe(r => { + r.a.toExponential(); + r.b.toLowerCase(); + }); + }); + }); + + it('should accept R typed reducers when R is assignable to T', () => { type(() => { let a: Rx.Observable<{ a?: number; b?: string }>; - a.reduce((acc, value) => { + const reduced = a.reduce((acc, value) => { value.a = acc.a; value.b = acc.b; return acc; }, {}); + + reduced.subscribe(r => { + r.a.toExponential(); + r.b.toLowerCase(); + }); + }); + }); + + it('should accept R typed reducers when R is not assignable to T', () => { + type(() => { + let a: Rx.Observable<{ a: number; b: string }>; + const seed = { + as: [1], + bs: ['a'] + }; + const reduced = a.reduce((acc, value) => { + acc.as.push(value.a); + acc.bs.push(value.b); + return acc; + }, seed); + + reduced.subscribe(r => { + r.as[0].toExponential(); + r.bs[0].toLowerCase(); + }); }); }); - it('should accept R typed reducers', () => { + it('should accept R typed reducers and reduce to type R', () => { type(() => { let a: Rx.Observable<{ a: number; b: string }>; - a.reduce<{ a?: number; b?: string }>((acc, value) => { + const reduced = a.reduce<{ a?: number; b?: string }>((acc, value) => { value.a = acc.a; value.b = acc.b; return acc; }, {}); + + reduced.subscribe(r => { + r.a.toExponential(); + r.b.toLowerCase(); + }); + }); + }); + + it('should accept array of R typed reducers and reduce to array of R', () => { + type(() => { + let a: Rx.Observable; + const reduced = a.reduce((acc, cur) => { + console.log(acc); + acc.push(cur.toString()); + return acc; + }, [] as string[]); + + reduced.subscribe(rs => { + rs[0].toLowerCase(); + }); }); }); -}); \ No newline at end of file +}); diff --git a/src/operator/reduce.ts b/src/operator/reduce.ts index 10b98b62b2..aefb943955 100644 --- a/src/operator/reduce.ts +++ b/src/operator/reduce.ts @@ -3,9 +3,9 @@ import { Operator } from '../Operator'; import { Subscriber } from '../Subscriber'; /* tslint:disable:max-line-length */ +export function reduce(this: Observable, accumulator: (acc: T[], value: T, index: number) => T[], seed: T[]): Observable; export function reduce(this: Observable, accumulator: (acc: T, value: T, index: number) => T, seed?: T): Observable; -export function reduce(this: Observable, accumulator: (acc: T[], value: T, index: number) => T[], seed?: T[]): Observable; -export function reduce(this: Observable, accumulator: (acc: R, value: T, index: number) => R, seed?: R): Observable; +export function reduce(this: Observable, accumulator: (acc: R, value: T, index: number) => R, seed: R): Observable; /* tslint:enable:max-line-length */ /**