diff --git a/spec-dtslint/operators/switchMap-spec.ts b/spec-dtslint/operators/switchMap-spec.ts index 66120a39c7..12a50a495c 100644 --- a/spec-dtslint/operators/switchMap-spec.ts +++ b/spec-dtslint/operators/switchMap-spec.ts @@ -6,7 +6,7 @@ it('should infer correctly', () => { }); it('should support a projector that takes an index', () => { - const o = of(1, 2, 3).pipe(switchMap((p, index) => of(Boolean(p)))); // $ExpectType Observable + const o = of(1, 2, 3).pipe(switchMap(p => of(Boolean(p)))); // $ExpectType Observable }); it('should infer correctly by using the resultSelector first parameter', () => { @@ -18,11 +18,11 @@ it('should infer correctly by using the resultSelector second parameter', () => }); it('should support a resultSelector that takes an inner index', () => { - const o = of(1, 2, 3).pipe(switchMap(p => of(Boolean(p)), (a, b, innnerIndex) => a)); // $ExpectType Observable + const o = of(1, 2, 3).pipe(switchMap(p => of(Boolean(p)), a => a)); // $ExpectType Observable }); it('should support a resultSelector that takes an inner and outer index', () => { - const o = of(1, 2, 3).pipe(switchMap(p => of(Boolean(p)), (a, b, innnerIndex, outerIndex) => a)); // $ExpectType Observable + const o = of(1, 2, 3).pipe(switchMap(p => of(Boolean(p)), a => a)); // $ExpectType Observable }); it('should support an undefined resultSelector', () => { @@ -36,3 +36,7 @@ it('should enforce types', () => { it('should enforce the return type', () => { const o = of(1, 2, 3).pipe(switchMap(p => p)); // $ExpectError }); + +it('should support projecting to union types', () => { + const o = of(Math.random()).pipe(switchMap(n => n > 0.5 ? of(123) : of('test'))); // $ExpectType Observable +}); diff --git a/src/internal/operators/switchMap.ts b/src/internal/operators/switchMap.ts index 0008c379ef..fdfadc0a87 100644 --- a/src/internal/operators/switchMap.ts +++ b/src/internal/operators/switchMap.ts @@ -5,16 +5,16 @@ import { Subscription } from '../Subscription'; import { OuterSubscriber } from '../OuterSubscriber'; import { InnerSubscriber } from '../InnerSubscriber'; import { subscribeToResult } from '../util/subscribeToResult'; -import { ObservableInput, OperatorFunction } from '../types'; +import { ObservableInput, OperatorFunction, ObservedValueOf } from '../types'; import { map } from './map'; import { from } from '../observable/from'; /* tslint:disable:max-line-length */ -export function switchMap(project: (value: T, index: number) => ObservableInput): OperatorFunction; +export function switchMap>(project: (value: T, index: number) => O): OperatorFunction>; /** @deprecated resultSelector is no longer supported, use inner map instead */ -export function switchMap(project: (value: T, index: number) => ObservableInput, resultSelector: undefined): OperatorFunction; +export function switchMap>(project: (value: T, index: number) => O, resultSelector: undefined): OperatorFunction>; /** @deprecated resultSelector is no longer supported, use inner map instead */ -export function switchMap(project: (value: T, index: number) => ObservableInput, resultSelector: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R): OperatorFunction; +export function switchMap>(project: (value: T, index: number) => O, resultSelector: (outerValue: T, innerValue: ObservedValueOf, outerIndex: number, innerIndex: number) => R): OperatorFunction; /* tslint:enable:max-line-length */ /** @@ -59,10 +59,10 @@ export function switchMap(project: (value: T, index: number) => Observa * @method switchMap * @owner Observable */ -export function switchMap( - project: (value: T, index: number) => ObservableInput, - resultSelector?: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R, -): OperatorFunction { +export function switchMap>( + project: (value: T, index: number) => O, + resultSelector?: (outerValue: T, innerValue: ObservedValueOf, outerIndex: number, innerIndex: number) => R, +): OperatorFunction|R> { if (typeof resultSelector === 'function') { return (source: Observable) => source.pipe( switchMap((a, i) => from(project(a, i)).pipe(