diff --git a/spec/operators/reduce-spec.ts b/spec/operators/reduce-spec.ts index ba3887ba94..654817544a 100644 --- a/spec/operators/reduce-spec.ts +++ b/spec/operators/reduce-spec.ts @@ -1,5 +1,5 @@ import * as Rx from '../../dist/cjs/Rx'; -declare const {hot, cold, asDiagram, expectObservable, expectSubscriptions}; +declare const {hot, cold, asDiagram, expectObservable, expectSubscriptions, type}; const Observable = Rx.Observable; @@ -231,4 +231,33 @@ describe('Observable.prototype.reduce', () => { expectObservable(e1.reduce(reduceFunction)).toBe(expected); expectSubscriptions(e1.subscriptions).toBe(e1subs); }); + + it('should accept array typed reducers', () => { + type(() => { + let a: Rx.Observable<{ a: number; b: string }>; + a.reduce((acc, value) => acc.concat(value), []); + }); + }); + + it('should accept T typed reducers', () => { + type(() => { + let a: Rx.Observable<{ a?: number; b?: string }>; + a.reduce((acc, value) => { + value.a = acc.a; + value.b = acc.b; + return acc; + }, {}); + }); + }); + + it('should accept R typed reducers', () => { + type(() => { + let a: Rx.Observable<{ a: number; b: string }>; + a.reduce<{ a?: number; b?: string }>((acc, value) => { + value.a = acc.a; + value.b = acc.b; + return acc; + }, {}); + }); + }); }); \ No newline at end of file diff --git a/spec/operators/scan-spec.ts b/spec/operators/scan-spec.ts index ef8eeb25e8..18755506c1 100644 --- a/spec/operators/scan-spec.ts +++ b/spec/operators/scan-spec.ts @@ -1,6 +1,6 @@ import {expect} from 'chai'; import * as Rx from '../../dist/cjs/Rx'; -declare const {hot, cold, asDiagram, expectObservable, expectSubscriptions}; +declare const {hot, cold, asDiagram, expectObservable, expectSubscriptions, type}; const Observable = Rx.Observable; @@ -201,4 +201,33 @@ describe('Observable.prototype.scan', () => { expectObservable(scan).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(e1subs); }); + + it('should accept array typed reducers', () => { + type(() => { + let a: Rx.Observable<{ a: number; b: string }>; + a.reduce((acc, value) => acc.concat(value), []); + }); + }); + + it('should accept T typed reducers', () => { + type(() => { + let a: Rx.Observable<{ a?: number; b?: string }>; + a.reduce((acc, value) => { + value.a = acc.a; + value.b = acc.b; + return acc; + }, {}); + }); + }); + + it('should accept R typed reducers', () => { + type(() => { + let a: Rx.Observable<{ a: number; b: string }>; + a.reduce<{ a?: number; b?: string }>((acc, value) => { + value.a = acc.a; + value.b = acc.b; + return acc; + }, {}); + }); + }); }); diff --git a/src/add/operator/scan.ts b/src/add/operator/scan.ts index a03f366dd9..46d99ac832 100644 --- a/src/add/operator/scan.ts +++ b/src/add/operator/scan.ts @@ -1,11 +1,12 @@ import {Observable} from '../../Observable'; -import {scan, ScanSignature} from '../../operator/scan'; +import {ReduceSignature} from '../../operator/reduce'; +import {scan} from '../../operator/scan'; Observable.prototype.scan = scan; declare module '../../Observable' { interface Observable { - scan: ScanSignature; + scan: ReduceSignature; } } \ No newline at end of file diff --git a/src/operator/reduce.ts b/src/operator/reduce.ts index 0e6168beec..014fb4e1cb 100644 --- a/src/operator/reduce.ts +++ b/src/operator/reduce.ts @@ -52,7 +52,9 @@ export function reduce(accumulator: (acc: R, value: T) => R, seed?: R): Ob } export interface ReduceSignature { - (accumulator: (acc: R, value: T) => R, seed?: R): Observable; + (accumulator: (acc: T, value: T, index: number) => T, seed?: T): Observable; + (accumulator: (acc: T[], value: T, index: number) => T[], seed?: T[]): Observable; + (accumulator: (acc: R, value: T, index: number) => R, seed?: R): Observable; } export class ReduceOperator implements Operator { diff --git a/src/operator/scan.ts b/src/operator/scan.ts index a85ea03fa8..b1bfd59a95 100644 --- a/src/operator/scan.ts +++ b/src/operator/scan.ts @@ -43,10 +43,6 @@ export function scan(accumulator: (acc: R, value: T, index: number) => R, return this.lift(new ScanOperator(accumulator, seed)); } -export interface ScanSignature { - (accumulator: (acc: R, value: T, index: number) => R, seed?: T | R): Observable; -} - class ScanOperator implements Operator { constructor(private accumulator: (acc: R, value: T, index: number) => R, private seed?: T | R) { } @@ -75,7 +71,7 @@ class ScanSubscriber extends Subscriber { this._seed = value; } - constructor(destination: Subscriber, private accumulator: (acc: R, value: T, index: number) => R, seed?: T|R) { + constructor(destination: Subscriber, private accumulator: (acc: R, value: T, index: number) => R, seed?: T | R) { super(destination); this.seed = seed; this.accumulatorSet = typeof seed !== 'undefined';