Skip to content

Commit e9e9041

Browse files
committed
fix(types): support returning union types from multicast projections
1 parent 1e19a24 commit e9e9041

File tree

6 files changed

+25
-11
lines changed

6 files changed

+25
-11
lines changed

spec-dtslint/operators/multicast-spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ it('should be possible to use a selector', () => {
1717
const r = of(1, 2, 3).pipe(multicast(() => new Subject<number>(), p => of('foo'))); // $ExpectType Observable<string>
1818
});
1919

20+
it('should support union types', () => {
21+
const o = of(1, 2, 3).pipe(multicast(new Subject<number>(), p => Math.random() > 0.5 ? of(123) : of('foo'))); // $ExpectType Observable<string | number>
22+
const p = of(1, 2, 3).pipe(multicast(() => new Subject<number>(), p => Math.random() > 0.5 ? of(123) : of('foo'))); // $ExpectType Observable<string | number>
23+
});
24+
2025
it('should enforce types', () => {
2126
const p = of(1, 2, 3).pipe(multicast()); // $ExpectError
2227
});

spec-dtslint/operators/publish-spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ it('should infer correctly with parameter', () => {
2222
it('should enforce type on selector', () => {
2323
const a = of(1, 2, 3).pipe(publish((x: Observable<string>) => x)); // $ExpectError
2424
});
25+
26+
it('should support union types in selector', () => {
27+
const a = of(1, 2, 3).pipe(publish(() => Math.random() > 0.5 ? of(123) : of('test'))); // $ExpectType Observable<string | number>
28+
});

spec-dtslint/operators/publishReplay-spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,18 @@ it('should accept windowTime, bufferSize, selector of OperatorFunction', () => {
2121
const a = of(1, 2, 3).pipe(publishReplay(1, 1, (x) => of('a'))); // $ExpectType Observable<string>
2222
});
2323

24+
it('should accept windowTime, bufferSize, selector returning union type', () => {
25+
const a = of(1, 2, 3).pipe(publishReplay(1, 1, () => Math.random() > 0.5 ? of(123) : of('test'))); // $ExpectType Observable<string | number>
26+
});
27+
2428
it('should accept windowTime, bufferSize, selector of MonoTypeOperatorFunction', () => {
2529
const a = of(1, 2, 3).pipe(publishReplay(1, 1, (x) => x)); // $ExpectType Observable<number>
2630
});
2731

32+
it('should accept windowTime, bufferSize, selector returning union type, and a scheduler', () => {
33+
const a = of(1, 2, 3).pipe(publishReplay(1, 1, () => Math.random() > 0.5 ? of(123) : of('test'), asyncScheduler)); // $ExpectType Observable<string | number>
34+
});
35+
2836
it('should accept windowTime, bufferSize, selector of OperatorFunction, and scheduler', () => {
2937
const a = of(1, 2, 3).pipe(publishReplay(1, 1, (x) => of('a'), asyncScheduler)); // $ExpectType Observable<string>
3038
});

src/internal/operators/multicast.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ import { Operator } from '../Operator';
33
import { Subscriber } from '../Subscriber';
44
import { Observable } from '../Observable';
55
import { ConnectableObservable, connectableObservableDescriptor } from '../observable/ConnectableObservable';
6-
import { MonoTypeOperatorFunction, OperatorFunction, UnaryFunction } from '../types';
6+
import { MonoTypeOperatorFunction, OperatorFunction, UnaryFunction, ObservedValueOf, ObservableInput } from '../types';
77

88
/* tslint:disable:max-line-length */
99
export function multicast<T>(subject: Subject<T>): UnaryFunction<Observable<T>, ConnectableObservable<T>>;
10-
export function multicast<T>(subject: Subject<T>, selector?: MonoTypeOperatorFunction<T>): MonoTypeOperatorFunction<T>;
11-
export function multicast<T, R>(subject: Subject<T>, selector?: OperatorFunction<T, R>): OperatorFunction<T, R>;
12-
export function multicast<T>(SubjectFactory: (this: Observable<T>) => Subject<T>): UnaryFunction<Observable<T>, ConnectableObservable<T>>;
13-
export function multicast<T>(SubjectFactory: (this: Observable<T>) => Subject<T>, selector?: MonoTypeOperatorFunction<T>): MonoTypeOperatorFunction<T>;
14-
export function multicast<T, R>(SubjectFactory: (this: Observable<T>) => Subject<T>, selector?: OperatorFunction<T, R>): OperatorFunction<T, R>;
10+
export function multicast<T, O extends ObservableInput<any>>(subject: Subject<T>, selector: (shared: Observable<T>) => O): UnaryFunction<Observable<T>, ConnectableObservable<ObservedValueOf<O>>>;
11+
export function multicast<T>(subjectFactory: (this: Observable<T>) => Subject<T>): UnaryFunction<Observable<T>, ConnectableObservable<T>>;
12+
export function multicast<T, O extends ObservableInput<any>>(SubjectFactory: (this: Observable<T>) => Subject<T>, selector: (shared: Observable<T>) => O): OperatorFunction<T, ObservedValueOf<O>>;
1513
/* tslint:enable:max-line-length */
1614

1715
/**

src/internal/operators/publish.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { Observable } from '../Observable';
22
import { Subject } from '../Subject';
33
import { multicast } from './multicast';
44
import { ConnectableObservable } from '../observable/ConnectableObservable';
5-
import { MonoTypeOperatorFunction, OperatorFunction, UnaryFunction } from '../types';
5+
import { MonoTypeOperatorFunction, OperatorFunction, UnaryFunction, ObservableInput, ObservedValueOf } from '../types';
66

77
/* tslint:disable:max-line-length */
88
export function publish<T>(): UnaryFunction<Observable<T>, ConnectableObservable<T>>;
9-
export function publish<T, R>(selector: OperatorFunction<T, R>): OperatorFunction<T, R>;
9+
export function publish<T, O extends ObservableInput<any>>(selector: (shared: Observable<T>) => O): OperatorFunction<T, ObservedValueOf<O>>;
1010
export function publish<T>(selector: MonoTypeOperatorFunction<T>): MonoTypeOperatorFunction<T>;
1111
/* tslint:enable:max-line-length */
1212

src/internal/operators/publishReplay.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ import { Observable } from '../Observable';
22
import { ReplaySubject } from '../ReplaySubject';
33
import { multicast } from './multicast';
44
import { ConnectableObservable } from '../observable/ConnectableObservable';
5-
import { UnaryFunction, MonoTypeOperatorFunction, OperatorFunction, SchedulerLike } from '../types';
5+
import { UnaryFunction, MonoTypeOperatorFunction, OperatorFunction, SchedulerLike, ObservableInput, ObservedValueOf } from '../types';
66

77
/* tslint:disable:max-line-length */
88
export function publishReplay<T>(bufferSize?: number, windowTime?: number, scheduler?: SchedulerLike): MonoTypeOperatorFunction<T>;
9-
export function publishReplay<T, R>(bufferSize?: number, windowTime?: number, selector?: OperatorFunction<T, R>, scheduler?: SchedulerLike): OperatorFunction<T, R>;
10-
export function publishReplay<T>(bufferSize?: number, windowTime?: number, selector?: MonoTypeOperatorFunction<T>, scheduler?: SchedulerLike): MonoTypeOperatorFunction<T>;
9+
export function publishReplay<T, O extends ObservableInput<any>>(bufferSize?: number, windowTime?: number, selector?: (shared: Observable<T>) => O, scheduler?: SchedulerLike): OperatorFunction<T, ObservedValueOf<O>>;
1110
/* tslint:enable:max-line-length */
1211

1312
export function publishReplay<T, R>(bufferSize?: number,

0 commit comments

Comments
 (0)