Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve type inference with zen-observable #7472

Open
wants to merge 2 commits into
base: 7.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions spec-dtslint/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,35 @@ export const g$ = of(new G());
export const h$ = of(new H());
export const i$ = of(new I());
export const j$ = of(new J());

interface Subscription {
closed: boolean;
unsubscribe(): void;
}
interface SubscriptionObserver<T> {
closed: boolean;
next(value: T): void;
error(errorValue: any): void;
complete(): void;
}
type Subscriber<T> = (observer: SubscriptionObserver<T>) => void | (() => void) | Subscription;

interface Observer<T> {
start?(subscription: Subscription): any;
next?(value: T): void;
error?(errorValue: any): void;
complete?(): void;
}

export declare class CompatObservable<T> {
constructor(subscriber: Subscriber<T>);

subscribe(observer: Observer<T>): Subscription;
subscribe(
onNext: (value: T) => void,
onError?: (error: any) => void,
onComplete?: () => void,
): Subscription;

[Symbol.observable](): CompatObservable<T>;
}
7 changes: 6 additions & 1 deletion spec-dtslint/types-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Head,
Tail
} from 'rxjs';
import { A, B, C } from './helpers';
import { A, B, C, CompatObservable } from './helpers';

describe('ObservedValueOf', () => {
it('should infer from an observable', () => {
Expand All @@ -24,6 +24,11 @@ describe('ObservedValueOf', () => {
let explicit: ObservedValueOf<Promise<A>>;
let inferred = explicit!; // $ExpectType A
});

it('should infer from an compatible observable', () => {
let explicit: ObservedValueOf<CompatObservable<A>>;
const inferred = explicit!; // $ExpectType A
});
});

describe('ObservedUnionFromArray', () => {
Expand Down
7 changes: 6 additions & 1 deletion src/internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ export interface Subscribable<T> {
subscribe(observer: Partial<Observer<T>>): Unsubscribable;
}

export interface InteropSubscribable<T> {
subscribe(observer: Partial<Observer<T>>): Unsubscribable;
subscribe(onNext?: (value: T) => void, onError?: (error: any) => void, onComplete?: () => void): Subscription;
}

/**
* Valid types that can be converted to observables.
*/
Expand All @@ -118,7 +123,7 @@ export type ObservableLike<T> = InteropObservable<T>;
* An object that implements the `Symbol.observable` interface.
*/
export interface InteropObservable<T> {
[Symbol.observable]: () => Subscribable<T>;
[Symbol.observable]: () => Subscribable<T> | InteropSubscribable<T>;
}

/* NOTIFICATIONS */
Expand Down