Skip to content

Commit 8350622

Browse files
committed
fix(types): support union type returns from catchError
1 parent 32d35fd commit 8350622

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

spec-dtslint/operators/catchError-spec.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import { of, Observable } from 'rxjs';
1+
import { of, Observable, EMPTY } from 'rxjs';
22
import { catchError } from 'rxjs/operators';
33

44
it('should infer correctly', () => {
55
const o = of(1, 2, 3).pipe(catchError((() => of(4, 5, 6)))); // $ExpectType Observable<number>
66
});
77

8+
it('should handle empty (never) appropriately', () => {
9+
const o = of(1, 2, 3).pipe(catchError(() => EMPTY));
10+
});
11+
812
it('should infer correctly when not returning', () => {
913
const o = of(1, 2, 3).pipe(catchError((() => { throw new Error('your hands in the air'); }))); // $ExpectType Observable<number>
1014
});
@@ -24,3 +28,7 @@ it('should enforce that selector returns an Observable', () => {
2428
it('should enforce type of caught', () => {
2529
const o = of(1, 2, 3).pipe(catchError((err, caught: Observable<string>) => of('a', 'b', 'c'))); // $ExpectError
2630
});
31+
32+
it('should handle union types', () => {
33+
const o = of(1, 2, 3).pipe(catchError(err => err.message === 'wee' ? of('fun') : of(123))); // $ExpectType Observable<string | number>
34+
});

src/internal/operators/catchError.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import {Observable} from '../Observable';
55
import {OuterSubscriber} from '../OuterSubscriber';
66
import { InnerSubscriber } from '../InnerSubscriber';
77
import {subscribeToResult} from '../util/subscribeToResult';
8-
import {ObservableInput, OperatorFunction, MonoTypeOperatorFunction} from '../types';
8+
import {ObservableInput, OperatorFunction, MonoTypeOperatorFunction, ObservedValueOf} from '../types';
99

10-
export function catchError<T>(selector: (err: any, caught: Observable<T>) => never): MonoTypeOperatorFunction<T>;
11-
export function catchError<T, R>(selector: (err: any, caught: Observable<T>) => ObservableInput<R>): OperatorFunction<T, T | R>;
10+
/* tslint:disable:max-line-length */
11+
export function catchError<T, O extends ObservableInput<any>>(selector: (err: any, caught: Observable<T>) => O): OperatorFunction<T, T | ObservedValueOf<O>>;
12+
/* tslint:enable:max-line-length */
1213

1314
/**
1415
* Catches errors on the observable to be handled by returning a new observable or throwing an error.
@@ -77,8 +78,10 @@ export function catchError<T, R>(selector: (err: any, caught: Observable<T>) =>
7778
* catch `selector` function.
7879
* @name catchError
7980
*/
80-
export function catchError<T, R>(selector: (err: any, caught: Observable<T>) => ObservableInput<R>): OperatorFunction<T, T | R> {
81-
return function catchErrorOperatorFunction(source: Observable<T>): Observable<T | R> {
81+
export function catchError<T, O extends ObservableInput<any>>(
82+
selector: (err: any, caught: Observable<T>) => O
83+
): OperatorFunction<T, T | ObservedValueOf<O>> {
84+
return function catchErrorOperatorFunction(source: Observable<T>): Observable<T | ObservedValueOf<O>> {
8285
const operator = new CatchOperator(selector);
8386
const caught = source.lift(operator);
8487
return (operator.caught = caught as Observable<T>);

0 commit comments

Comments
 (0)