diff --git a/spec-dtslint/operators/catchError-spec.ts b/spec-dtslint/operators/catchError-spec.ts index 5a3b3f2838..dab0ebec49 100644 --- a/spec-dtslint/operators/catchError-spec.ts +++ b/spec-dtslint/operators/catchError-spec.ts @@ -1,10 +1,14 @@ -import { of, Observable } from 'rxjs'; +import { of, Observable, EMPTY } from 'rxjs'; import { catchError } from 'rxjs/operators'; it('should infer correctly', () => { const o = of(1, 2, 3).pipe(catchError((() => of(4, 5, 6)))); // $ExpectType Observable }); +it('should handle empty (never) appropriately', () => { + const o = of(1, 2, 3).pipe(catchError(() => EMPTY)); +}); + it('should infer correctly when not returning', () => { const o = of(1, 2, 3).pipe(catchError((() => { throw new Error('your hands in the air'); }))); // $ExpectType Observable }); @@ -24,3 +28,7 @@ it('should enforce that selector returns an Observable', () => { it('should enforce type of caught', () => { const o = of(1, 2, 3).pipe(catchError((err, caught: Observable) => of('a', 'b', 'c'))); // $ExpectError }); + +it('should handle union types', () => { + const o = of(1, 2, 3).pipe(catchError(err => err.message === 'wee' ? of('fun') : of(123))); // $ExpectType Observable +}); diff --git a/src/internal/operators/catchError.ts b/src/internal/operators/catchError.ts index 6d259b5769..48d6040a0c 100644 --- a/src/internal/operators/catchError.ts +++ b/src/internal/operators/catchError.ts @@ -5,10 +5,11 @@ import {Observable} from '../Observable'; import {OuterSubscriber} from '../OuterSubscriber'; import { InnerSubscriber } from '../InnerSubscriber'; import {subscribeToResult} from '../util/subscribeToResult'; -import {ObservableInput, OperatorFunction, MonoTypeOperatorFunction} from '../types'; +import {ObservableInput, OperatorFunction, MonoTypeOperatorFunction, ObservedValueOf} from '../types'; -export function catchError(selector: (err: any, caught: Observable) => never): MonoTypeOperatorFunction; -export function catchError(selector: (err: any, caught: Observable) => ObservableInput): OperatorFunction; +/* tslint:disable:max-line-length */ +export function catchError>(selector: (err: any, caught: Observable) => O): OperatorFunction>; +/* tslint:enable:max-line-length */ /** * Catches errors on the observable to be handled by returning a new observable or throwing an error. @@ -77,8 +78,10 @@ export function catchError(selector: (err: any, caught: Observable) => * catch `selector` function. * @name catchError */ -export function catchError(selector: (err: any, caught: Observable) => ObservableInput): OperatorFunction { - return function catchErrorOperatorFunction(source: Observable): Observable { +export function catchError>( + selector: (err: any, caught: Observable) => O +): OperatorFunction> { + return function catchErrorOperatorFunction(source: Observable): Observable> { const operator = new CatchOperator(selector); const caught = source.lift(operator); return (operator.caught = caught as Observable);