From e9e90411a64f6b8b411fa493ab6657f840a18f3f Mon Sep 17 00:00:00 2001 From: Ben Lesh Date: Tue, 8 Jan 2019 23:59:10 -0800 Subject: [PATCH] fix(types): support returning union types from multicast projections --- spec-dtslint/operators/multicast-spec.ts | 5 +++++ spec-dtslint/operators/publish-spec.ts | 4 ++++ spec-dtslint/operators/publishReplay-spec.ts | 8 ++++++++ src/internal/operators/multicast.ts | 10 ++++------ src/internal/operators/publish.ts | 4 ++-- src/internal/operators/publishReplay.ts | 5 ++--- 6 files changed, 25 insertions(+), 11 deletions(-) diff --git a/spec-dtslint/operators/multicast-spec.ts b/spec-dtslint/operators/multicast-spec.ts index eb0673def7..4be93fbdd3 100644 --- a/spec-dtslint/operators/multicast-spec.ts +++ b/spec-dtslint/operators/multicast-spec.ts @@ -17,6 +17,11 @@ it('should be possible to use a selector', () => { const r = of(1, 2, 3).pipe(multicast(() => new Subject(), p => of('foo'))); // $ExpectType Observable }); +it('should support union types', () => { + const o = of(1, 2, 3).pipe(multicast(new Subject(), p => Math.random() > 0.5 ? of(123) : of('foo'))); // $ExpectType Observable + const p = of(1, 2, 3).pipe(multicast(() => new Subject(), p => Math.random() > 0.5 ? of(123) : of('foo'))); // $ExpectType Observable +}); + it('should enforce types', () => { const p = of(1, 2, 3).pipe(multicast()); // $ExpectError }); diff --git a/spec-dtslint/operators/publish-spec.ts b/spec-dtslint/operators/publish-spec.ts index 3685379593..307d6192da 100644 --- a/spec-dtslint/operators/publish-spec.ts +++ b/spec-dtslint/operators/publish-spec.ts @@ -22,3 +22,7 @@ it('should infer correctly with parameter', () => { it('should enforce type on selector', () => { const a = of(1, 2, 3).pipe(publish((x: Observable) => x)); // $ExpectError }); + +it('should support union types in selector', () => { + const a = of(1, 2, 3).pipe(publish(() => Math.random() > 0.5 ? of(123) : of('test'))); // $ExpectType Observable +}); diff --git a/spec-dtslint/operators/publishReplay-spec.ts b/spec-dtslint/operators/publishReplay-spec.ts index 32a56057cb..943aed4056 100644 --- a/spec-dtslint/operators/publishReplay-spec.ts +++ b/spec-dtslint/operators/publishReplay-spec.ts @@ -21,10 +21,18 @@ it('should accept windowTime, bufferSize, selector of OperatorFunction', () => { const a = of(1, 2, 3).pipe(publishReplay(1, 1, (x) => of('a'))); // $ExpectType Observable }); +it('should accept windowTime, bufferSize, selector returning union type', () => { + const a = of(1, 2, 3).pipe(publishReplay(1, 1, () => Math.random() > 0.5 ? of(123) : of('test'))); // $ExpectType Observable +}); + it('should accept windowTime, bufferSize, selector of MonoTypeOperatorFunction', () => { const a = of(1, 2, 3).pipe(publishReplay(1, 1, (x) => x)); // $ExpectType Observable }); +it('should accept windowTime, bufferSize, selector returning union type, and a scheduler', () => { + const a = of(1, 2, 3).pipe(publishReplay(1, 1, () => Math.random() > 0.5 ? of(123) : of('test'), asyncScheduler)); // $ExpectType Observable +}); + it('should accept windowTime, bufferSize, selector of OperatorFunction, and scheduler', () => { const a = of(1, 2, 3).pipe(publishReplay(1, 1, (x) => of('a'), asyncScheduler)); // $ExpectType Observable }); diff --git a/src/internal/operators/multicast.ts b/src/internal/operators/multicast.ts index c79b4e8795..97d28d5578 100644 --- a/src/internal/operators/multicast.ts +++ b/src/internal/operators/multicast.ts @@ -3,15 +3,13 @@ import { Operator } from '../Operator'; import { Subscriber } from '../Subscriber'; import { Observable } from '../Observable'; import { ConnectableObservable, connectableObservableDescriptor } from '../observable/ConnectableObservable'; -import { MonoTypeOperatorFunction, OperatorFunction, UnaryFunction } from '../types'; +import { MonoTypeOperatorFunction, OperatorFunction, UnaryFunction, ObservedValueOf, ObservableInput } from '../types'; /* tslint:disable:max-line-length */ export function multicast(subject: Subject): UnaryFunction, ConnectableObservable>; -export function multicast(subject: Subject, selector?: MonoTypeOperatorFunction): MonoTypeOperatorFunction; -export function multicast(subject: Subject, selector?: OperatorFunction): OperatorFunction; -export function multicast(SubjectFactory: (this: Observable) => Subject): UnaryFunction, ConnectableObservable>; -export function multicast(SubjectFactory: (this: Observable) => Subject, selector?: MonoTypeOperatorFunction): MonoTypeOperatorFunction; -export function multicast(SubjectFactory: (this: Observable) => Subject, selector?: OperatorFunction): OperatorFunction; +export function multicast>(subject: Subject, selector: (shared: Observable) => O): UnaryFunction, ConnectableObservable>>; +export function multicast(subjectFactory: (this: Observable) => Subject): UnaryFunction, ConnectableObservable>; +export function multicast>(SubjectFactory: (this: Observable) => Subject, selector: (shared: Observable) => O): OperatorFunction>; /* tslint:enable:max-line-length */ /** diff --git a/src/internal/operators/publish.ts b/src/internal/operators/publish.ts index ff8834c026..6cb6fb913c 100644 --- a/src/internal/operators/publish.ts +++ b/src/internal/operators/publish.ts @@ -2,11 +2,11 @@ import { Observable } from '../Observable'; import { Subject } from '../Subject'; import { multicast } from './multicast'; import { ConnectableObservable } from '../observable/ConnectableObservable'; -import { MonoTypeOperatorFunction, OperatorFunction, UnaryFunction } from '../types'; +import { MonoTypeOperatorFunction, OperatorFunction, UnaryFunction, ObservableInput, ObservedValueOf } from '../types'; /* tslint:disable:max-line-length */ export function publish(): UnaryFunction, ConnectableObservable>; -export function publish(selector: OperatorFunction): OperatorFunction; +export function publish>(selector: (shared: Observable) => O): OperatorFunction>; export function publish(selector: MonoTypeOperatorFunction): MonoTypeOperatorFunction; /* tslint:enable:max-line-length */ diff --git a/src/internal/operators/publishReplay.ts b/src/internal/operators/publishReplay.ts index 68162b6aa3..a64b52a135 100644 --- a/src/internal/operators/publishReplay.ts +++ b/src/internal/operators/publishReplay.ts @@ -2,12 +2,11 @@ import { Observable } from '../Observable'; import { ReplaySubject } from '../ReplaySubject'; import { multicast } from './multicast'; import { ConnectableObservable } from '../observable/ConnectableObservable'; -import { UnaryFunction, MonoTypeOperatorFunction, OperatorFunction, SchedulerLike } from '../types'; +import { UnaryFunction, MonoTypeOperatorFunction, OperatorFunction, SchedulerLike, ObservableInput, ObservedValueOf } from '../types'; /* tslint:disable:max-line-length */ export function publishReplay(bufferSize?: number, windowTime?: number, scheduler?: SchedulerLike): MonoTypeOperatorFunction; -export function publishReplay(bufferSize?: number, windowTime?: number, selector?: OperatorFunction, scheduler?: SchedulerLike): OperatorFunction; -export function publishReplay(bufferSize?: number, windowTime?: number, selector?: MonoTypeOperatorFunction, scheduler?: SchedulerLike): MonoTypeOperatorFunction; +export function publishReplay>(bufferSize?: number, windowTime?: number, selector?: (shared: Observable) => O, scheduler?: SchedulerLike): OperatorFunction>; /* tslint:enable:max-line-length */ export function publishReplay(bufferSize?: number,