From f03e7907fc7ff848a4ab41092739a856a8e6d5cf Mon Sep 17 00:00:00 2001 From: Ben Lesh Date: Tue, 25 Sep 2018 13:42:08 -0700 Subject: [PATCH] fix(TypeScript): ensure RxJS builds with TS@next as well --- spec-dtslint/util/pipe-spec.ts | 23 +++++++++++++++++++---- spec/operators/zipAll-spec.ts | 7 +++---- src/internal/operators/reduce.ts | 7 ++++--- src/internal/util/pipe.ts | 25 +++++++++++-------------- 4 files changed, 37 insertions(+), 25 deletions(-) diff --git a/spec-dtslint/util/pipe-spec.ts b/spec-dtslint/util/pipe-spec.ts index b715aea054..4661852ed9 100644 --- a/spec-dtslint/util/pipe-spec.ts +++ b/spec-dtslint/util/pipe-spec.ts @@ -84,14 +84,29 @@ it('should enforce types for the 9th argument', () => { const o = pipe(a('0', '1'), a('1', '2'), a('2', '3'), a('3', '4'), a('4', '5'), a('5', '6'), a('6', '7'), a('7', '8'), a('#', '9')); // $ExpectError }); -it('should return generic OperatorFunction', () => { +it('should return generic OperatorFunction 1', () => { + const customOperator = (p: T) => (a: Observable) => a; + + const staticPipe = pipe(customOperator('infer')); + const o = of('foo').pipe(staticPipe); // $ExpectType Observable +}); + +it('should return generic OperatorFunction 2', () => { const customOperator = () => (a: Observable) => a; - const staticPipe = pipe(customOperator()); + const staticPipe = pipe(customOperator()); const o = of('foo').pipe(staticPipe); // $ExpectType Observable }); +it('should return generic OperatorFunction 3', () => { + const customOperator = () => (a: Observable) => a; + + // type can't be possibly be inferred here, so it's {} instead of T. + const staticPipe = pipe(customOperator()); + const o = of('foo').pipe(staticPipe); // $ExpectType Observable<{}> +}); + it('should return generic function', () => { - const func = pipe((value: string) => value); - const value = func("foo"); // $ExpectType "foo" + const func = pipe((value: string) => value, (value: string) => value + value); + const value = func("foo"); // $ExpectType string }); diff --git a/spec/operators/zipAll-spec.ts b/spec/operators/zipAll-spec.ts index 6b8d0af1c1..52a3c56f09 100644 --- a/spec/operators/zipAll-spec.ts +++ b/spec/operators/zipAll-spec.ts @@ -35,11 +35,10 @@ describe('zipAll operator', () => { it('should take all observables from the source and zip them', (done) => { const expected = ['a1', 'b2', 'c3']; let i = 0; - of( + const source = of( of('a', 'b', 'c'), of(1, 2, 3) - ) - .pipe(zipAll((a, b) => String(a) + String(b))) + ).pipe(zipAll((a: string, b: number) => a + b)) .subscribe((x) => { expect(x).to.equal(expected[i++]); }, null, done); @@ -378,7 +377,7 @@ describe('zipAll operator', () => { of('a', 'b', 'c'), of(1, 2) ) - .pipe(zipAll((a, b) => String(a) + String(b))) + .pipe(zipAll((a: string, b: number) => a + b)) .subscribe((x) => { expect(x).to.equal(expected[i++]); }, null, done); diff --git a/src/internal/operators/reduce.ts b/src/internal/operators/reduce.ts index 9820dd47b7..a627dfb4ef 100644 --- a/src/internal/operators/reduce.ts +++ b/src/internal/operators/reduce.ts @@ -71,8 +71,9 @@ export function reduce(accumulator: (acc: R, value: T, index?: number) => }; } return function reduceOperatorFunction(source: Observable): Observable { - return pipe(scan((acc, value, index) => { - return accumulator(acc, value, index + 1); - }), takeLast(1))(source) as Observable; + return pipe( + scan((acc: R, value: T, index: number): R => accumulator(acc, value, index + 1)), + takeLast(1), + )(source); }; } diff --git a/src/internal/util/pipe.ts b/src/internal/util/pipe.ts index 14c92cb976..206f566d82 100644 --- a/src/internal/util/pipe.ts +++ b/src/internal/util/pipe.ts @@ -1,21 +1,18 @@ import { noop } from './noop'; -import { UnaryFunction, MonoTypeOperatorFunction } from '../types'; -import { Observable } from '../Observable'; +import { UnaryFunction } from '../types'; /* tslint:disable:max-line-length */ export function pipe(): UnaryFunction; -export function pipe(...operators: MonoTypeOperatorFunction[]): (source: Observable) => Observable; -export function pipe(...operators: UnaryFunction[]): (source: R) => R; -export function pipe(op1: UnaryFunction): UnaryFunction; -export function pipe(op1: UnaryFunction, op2: UnaryFunction): UnaryFunction; -export function pipe(op1: UnaryFunction, op2: UnaryFunction, op3: UnaryFunction): UnaryFunction; -export function pipe(op1: UnaryFunction, op2: UnaryFunction, op3: UnaryFunction, op4: UnaryFunction): UnaryFunction; -export function pipe(op1: UnaryFunction, op2: UnaryFunction, op3: UnaryFunction, op4: UnaryFunction, op5: UnaryFunction): UnaryFunction; -export function pipe(op1: UnaryFunction, op2: UnaryFunction, op3: UnaryFunction, op4: UnaryFunction, op5: UnaryFunction, op6: UnaryFunction): UnaryFunction; -export function pipe(op1: UnaryFunction, op2: UnaryFunction, op3: UnaryFunction, op4: UnaryFunction, op5: UnaryFunction, op6: UnaryFunction, op7: UnaryFunction): UnaryFunction; -export function pipe(op1: UnaryFunction, op2: UnaryFunction, op3: UnaryFunction, op4: UnaryFunction, op5: UnaryFunction, op6: UnaryFunction, op7: UnaryFunction, op8: UnaryFunction): UnaryFunction; -export function pipe(op1: UnaryFunction, op2: UnaryFunction, op3: UnaryFunction, op4: UnaryFunction, op5: UnaryFunction, op6: UnaryFunction, op7: UnaryFunction, op8: UnaryFunction, op9: UnaryFunction): UnaryFunction; -export function pipe(op1: UnaryFunction, op2: UnaryFunction, op3: UnaryFunction, op4: UnaryFunction, op5: UnaryFunction, op6: UnaryFunction, op7: UnaryFunction, op8: UnaryFunction, op9: UnaryFunction, ...operations: UnaryFunction[]): UnaryFunction; +export function pipe(fn1: UnaryFunction): UnaryFunction; +export function pipe(fn1: UnaryFunction, fn2: UnaryFunction): UnaryFunction; +export function pipe(fn1: UnaryFunction, fn2: UnaryFunction, fn3: UnaryFunction): UnaryFunction; +export function pipe(fn1: UnaryFunction, fn2: UnaryFunction, fn3: UnaryFunction, fn4: UnaryFunction): UnaryFunction; +export function pipe(fn1: UnaryFunction, fn2: UnaryFunction, fn3: UnaryFunction, fn4: UnaryFunction, fn5: UnaryFunction): UnaryFunction; +export function pipe(fn1: UnaryFunction, fn2: UnaryFunction, fn3: UnaryFunction, fn4: UnaryFunction, fn5: UnaryFunction, fn6: UnaryFunction): UnaryFunction; +export function pipe(fn1: UnaryFunction, fn2: UnaryFunction, fn3: UnaryFunction, fn4: UnaryFunction, fn5: UnaryFunction, fn6: UnaryFunction, fn7: UnaryFunction): UnaryFunction; +export function pipe(fn1: UnaryFunction, fn2: UnaryFunction, fn3: UnaryFunction, fn4: UnaryFunction, fn5: UnaryFunction, fn6: UnaryFunction, fn7: UnaryFunction, fn8: UnaryFunction): UnaryFunction; +export function pipe(fn1: UnaryFunction, fn2: UnaryFunction, fn3: UnaryFunction, fn4: UnaryFunction, fn5: UnaryFunction, fn6: UnaryFunction, fn7: UnaryFunction, fn8: UnaryFunction, fn9: UnaryFunction): UnaryFunction; +export function pipe(fn1: UnaryFunction, fn2: UnaryFunction, fn3: UnaryFunction, fn4: UnaryFunction, fn5: UnaryFunction, fn6: UnaryFunction, fn7: UnaryFunction, fn8: UnaryFunction, fn9: UnaryFunction, ...fns: UnaryFunction[]): UnaryFunction; /* tslint:enable:max-line-length */ export function pipe(...fns: Array>): UnaryFunction {