Skip to content

Commit 32d35fd

Browse files
committed
fix(types): support union type returns in switchMap
1 parent c2ac39c commit 32d35fd

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

‎spec-dtslint/operators/switchMap-spec.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ it('should infer correctly', () => {
66
});
77

88
it('should support a projector that takes an index',()=>{
9-
consto=of(1,2,3).pipe(switchMap((p,index)=>of(Boolean(p))));// $ExpectType Observable<boolean>
9+
consto=of(1,2,3).pipe(switchMap(p=>of(Boolean(p))));// $ExpectType Observable<boolean>
1010
});
1111

1212
it('should infer correctly by using the resultSelector first parameter',()=>{
@@ -18,11 +18,11 @@ it('should infer correctly by using the resultSelector second parameter', () =>
1818
});
1919

2020
it('should support a resultSelector that takes an inner index',()=>{
21-
consto=of(1,2,3).pipe(switchMap(p=>of(Boolean(p)),(a,b,innnerIndex)=>a));// $ExpectType Observable<number>
21+
consto=of(1,2,3).pipe(switchMap(p=>of(Boolean(p)),a=>a));// $ExpectType Observable<number>
2222
});
2323

2424
it('should support a resultSelector that takes an inner and outer index',()=>{
25-
consto=of(1,2,3).pipe(switchMap(p=>of(Boolean(p)),(a,b,innnerIndex,outerIndex)=>a));// $ExpectType Observable<number>
25+
consto=of(1,2,3).pipe(switchMap(p=>of(Boolean(p)),a=>a));// $ExpectType Observable<number>
2626
});
2727

2828
it('should support an undefined resultSelector',()=>{
@@ -36,3 +36,7 @@ it('should enforce types', () => {
3636
it('should enforce the return type',()=>{
3737
consto=of(1,2,3).pipe(switchMap(p=>p));// $ExpectError
3838
});
39+
40+
it('should support projecting to union types',()=>{
41+
consto=of(Math.random()).pipe(switchMap(n=>n>0.5 ? of(123) : of('test')));// $ExpectType Observable<string | number>
42+
});

‎src/internal/operators/switchMap.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import { Subscription } from '../Subscription';
55
import{OuterSubscriber}from'../OuterSubscriber';
66
import{InnerSubscriber}from'../InnerSubscriber';
77
import{subscribeToResult}from'../util/subscribeToResult';
8-
import{ObservableInput,OperatorFunction}from'../types';
8+
import{ObservableInput,OperatorFunction,ObservedValueOf}from'../types';
99
import{map}from'./map';
1010
import{from}from'../observable/from';
1111

1212
/* tslint:disable:max-line-length */
13-
exportfunctionswitchMap<T,R>(project: (value: T,index: number)=>ObservableInput<R>): OperatorFunction<T,R>;
13+
exportfunctionswitchMap<T,OextendsObservableInput<any>>(project: (value: T,index: number)=>O): OperatorFunction<T,ObservedValueOf<O>>;
1414
/** @deprecated resultSelector is no longer supported, use inner map instead */
15-
exportfunctionswitchMap<T,R>(project: (value: T,index: number)=>ObservableInput<R>,resultSelector: undefined): OperatorFunction<T,R>;
15+
exportfunctionswitchMap<T,OextendsObservableInput<any>>(project: (value: T,index: number)=>O,resultSelector: undefined): OperatorFunction<T,ObservedValueOf<O>>;
1616
/** @deprecated resultSelector is no longer supported, use inner map instead */
17-
exportfunctionswitchMap<T,I,R>(project: (value: T,index: number)=>ObservableInput<I>,resultSelector: (outerValue: T,innerValue: I,outerIndex: number,innerIndex: number)=>R): OperatorFunction<T,R>;
17+
exportfunctionswitchMap<T,R,OextendsObservableInput<any>>(project: (value: T,index: number)=>O,resultSelector: (outerValue: T,innerValue: ObservedValueOf<O>,outerIndex: number,innerIndex: number)=>R): OperatorFunction<T,R>;
1818
/* tslint:enable:max-line-length */
1919

2020
/**
@@ -59,10 +59,10 @@ export function switchMap<T, I, R>(project: (value: T, index: number) => Observa
5959
* @method switchMap
6060
* @owner Observable
6161
*/
62-
exportfunctionswitchMap<T,I,R>(
63-
project: (value: T,index: number)=>ObservableInput<I>,
64-
resultSelector?: (outerValue: T,innerValue: I,outerIndex: number,innerIndex: number)=>R,
65-
): OperatorFunction<T,I|R>{
62+
exportfunctionswitchMap<T,R,OextendsObservableInput<any>>(
63+
project: (value: T,index: number)=>O,
64+
resultSelector?: (outerValue: T,innerValue: ObservedValueOf<O>,outerIndex: number,innerIndex: number)=>R,
65+
): OperatorFunction<T,ObservedValueOf<O>|R>{
6666
if(typeofresultSelector==='function'){
6767
return(source: Observable<T>)=>source.pipe(
6868
switchMap((a,i)=>from(project(a,i)).pipe(

0 commit comments

Comments
 (0)