Skip to content

Commit

Permalink
fix(TypeScript): ensure RxJS builds with TS@next as well
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Sep 25, 2018
1 parent 0972c56 commit f03e790
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
23 changes: 19 additions & 4 deletions spec-dtslint/util/pipe-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <T>(p: T) => (a: Observable<T>) => a;

const staticPipe = pipe(customOperator('infer'));
const o = of('foo').pipe(staticPipe); // $ExpectType Observable<string>
});

it('should return generic OperatorFunction 2', () => {
const customOperator = <T>() => (a: Observable<T>) => a;

const staticPipe = pipe(customOperator());
const staticPipe = pipe(customOperator<string>());
const o = of('foo').pipe(staticPipe); // $ExpectType Observable<string>
});

it('should return generic OperatorFunction 3', () => {
const customOperator = <T>() => (a: Observable<T>) => 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
});
7 changes: 3 additions & 4 deletions spec/operators/zipAll-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions src/internal/operators/reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ export function reduce<T, R>(accumulator: (acc: R, value: T, index?: number) =>
};
}
return function reduceOperatorFunction(source: Observable<T>): Observable<R> {
return pipe(scan<T, T | R>((acc, value, index) => {
return accumulator(<R>acc, value, index + 1);
}), takeLast(1))(source) as Observable<R>;
return pipe(
scan((acc: R, value: T, index: number): R => accumulator(acc, value, index + 1)),
takeLast(1),
)(source);
};
}
25 changes: 11 additions & 14 deletions src/internal/util/pipe.ts
Original file line number Diff line number Diff line change
@@ -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<T>(): UnaryFunction<T, T>;
export function pipe<T>(...operators: MonoTypeOperatorFunction<T>[]): <R extends T>(source: Observable<R>) => Observable<R>;
export function pipe<T>(...operators: UnaryFunction<T, T>[]): <R extends T>(source: R) => R;
export function pipe<T, A>(op1: UnaryFunction<T, A>): UnaryFunction<T, A>;
export function pipe<T, A, B>(op1: UnaryFunction<T, A>, op2: UnaryFunction<A, B>): UnaryFunction<T, B>;
export function pipe<T, A, B, C>(op1: UnaryFunction<T, A>, op2: UnaryFunction<A, B>, op3: UnaryFunction<B, C>): UnaryFunction<T, C>;
export function pipe<T, A, B, C, D>(op1: UnaryFunction<T, A>, op2: UnaryFunction<A, B>, op3: UnaryFunction<B, C>, op4: UnaryFunction<C, D>): UnaryFunction<T, D>;
export function pipe<T, A, B, C, D, E>(op1: UnaryFunction<T, A>, op2: UnaryFunction<A, B>, op3: UnaryFunction<B, C>, op4: UnaryFunction<C, D>, op5: UnaryFunction<D, E>): UnaryFunction<T, E>;
export function pipe<T, A, B, C, D, E, F>(op1: UnaryFunction<T, A>, op2: UnaryFunction<A, B>, op3: UnaryFunction<B, C>, op4: UnaryFunction<C, D>, op5: UnaryFunction<D, E>, op6: UnaryFunction<E, F>): UnaryFunction<T, F>;
export function pipe<T, A, B, C, D, E, F, G>(op1: UnaryFunction<T, A>, op2: UnaryFunction<A, B>, op3: UnaryFunction<B, C>, op4: UnaryFunction<C, D>, op5: UnaryFunction<D, E>, op6: UnaryFunction<E, F>, op7: UnaryFunction<F, G>): UnaryFunction<T, G>;
export function pipe<T, A, B, C, D, E, F, G, H>(op1: UnaryFunction<T, A>, op2: UnaryFunction<A, B>, op3: UnaryFunction<B, C>, op4: UnaryFunction<C, D>, op5: UnaryFunction<D, E>, op6: UnaryFunction<E, F>, op7: UnaryFunction<F, G>, op8: UnaryFunction<G, H>): UnaryFunction<T, H>;
export function pipe<T, A, B, C, D, E, F, G, H, I>(op1: UnaryFunction<T, A>, op2: UnaryFunction<A, B>, op3: UnaryFunction<B, C>, op4: UnaryFunction<C, D>, op5: UnaryFunction<D, E>, op6: UnaryFunction<E, F>, op7: UnaryFunction<F, G>, op8: UnaryFunction<G, H>, op9: UnaryFunction<H, I>): UnaryFunction<T, I>;
export function pipe<T, A, B, C, D, E, F, G, H, I>(op1: UnaryFunction<T, A>, op2: UnaryFunction<A, B>, op3: UnaryFunction<B, C>, op4: UnaryFunction<C, D>, op5: UnaryFunction<D, E>, op6: UnaryFunction<E, F>, op7: UnaryFunction<F, G>, op8: UnaryFunction<G, H>, op9: UnaryFunction<H, I>, ...operations: UnaryFunction<any, any>[]): UnaryFunction<T, {}>;
export function pipe<T, A>(fn1: UnaryFunction<T, A>): UnaryFunction<T, A>;
export function pipe<T, A, B>(fn1: UnaryFunction<T, A>, fn2: UnaryFunction<A, B>): UnaryFunction<T, B>;
export function pipe<T, A, B, C>(fn1: UnaryFunction<T, A>, fn2: UnaryFunction<A, B>, fn3: UnaryFunction<B, C>): UnaryFunction<T, C>;
export function pipe<T, A, B, C, D>(fn1: UnaryFunction<T, A>, fn2: UnaryFunction<A, B>, fn3: UnaryFunction<B, C>, fn4: UnaryFunction<C, D>): UnaryFunction<T, D>;
export function pipe<T, A, B, C, D, E>(fn1: UnaryFunction<T, A>, fn2: UnaryFunction<A, B>, fn3: UnaryFunction<B, C>, fn4: UnaryFunction<C, D>, fn5: UnaryFunction<D, E>): UnaryFunction<T, E>;
export function pipe<T, A, B, C, D, E, F>(fn1: UnaryFunction<T, A>, fn2: UnaryFunction<A, B>, fn3: UnaryFunction<B, C>, fn4: UnaryFunction<C, D>, fn5: UnaryFunction<D, E>, fn6: UnaryFunction<E, F>): UnaryFunction<T, F>;
export function pipe<T, A, B, C, D, E, F, G>(fn1: UnaryFunction<T, A>, fn2: UnaryFunction<A, B>, fn3: UnaryFunction<B, C>, fn4: UnaryFunction<C, D>, fn5: UnaryFunction<D, E>, fn6: UnaryFunction<E, F>, fn7: UnaryFunction<F, G>): UnaryFunction<T, G>;
export function pipe<T, A, B, C, D, E, F, G, H>(fn1: UnaryFunction<T, A>, fn2: UnaryFunction<A, B>, fn3: UnaryFunction<B, C>, fn4: UnaryFunction<C, D>, fn5: UnaryFunction<D, E>, fn6: UnaryFunction<E, F>, fn7: UnaryFunction<F, G>, fn8: UnaryFunction<G, H>): UnaryFunction<T, H>;
export function pipe<T, A, B, C, D, E, F, G, H, I>(fn1: UnaryFunction<T, A>, fn2: UnaryFunction<A, B>, fn3: UnaryFunction<B, C>, fn4: UnaryFunction<C, D>, fn5: UnaryFunction<D, E>, fn6: UnaryFunction<E, F>, fn7: UnaryFunction<F, G>, fn8: UnaryFunction<G, H>, fn9: UnaryFunction<H, I>): UnaryFunction<T, I>;
export function pipe<T, A, B, C, D, E, F, G, H, I>(fn1: UnaryFunction<T, A>, fn2: UnaryFunction<A, B>, fn3: UnaryFunction<B, C>, fn4: UnaryFunction<C, D>, fn5: UnaryFunction<D, E>, fn6: UnaryFunction<E, F>, fn7: UnaryFunction<F, G>, fn8: UnaryFunction<G, H>, fn9: UnaryFunction<H, I>, ...fns: UnaryFunction<any, any>[]): UnaryFunction<T, {}>;
/* tslint:enable:max-line-length */

export function pipe(...fns: Array<UnaryFunction<any, any>>): UnaryFunction<any, any> {
Expand Down

0 comments on commit f03e790

Please sign in to comment.