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

fix: type inference of type #833

Open
wants to merge 1 commit into
base: master
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 42 additions & 4 deletions src/operators/of-type.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ import { ofType } from './of-type';
import { IEvent } from '../interfaces';

describe('operators/ofType', () => {
class A implements IEvent {}
class B implements IEvent {}
class A implements IEvent {
keyAOrB: string = 'keyAOrB';
keyAOnly: string = 'keyAOnly';
}
class B implements IEvent {
keyAOrB: string = 'keyAOrB';
}
class SubA extends A {}
class C implements IEvent {}
class C implements IEvent {
keyCOptional?: string;
}

let stream: Subject<any>;
let output: IEvent[];
Expand All @@ -32,7 +39,7 @@ describe('operators/ofType', () => {
expectedResults.push(new A());

stream.next(new B());
stream.next(...expectedResults);
expectedResults.forEach((event) => stream.next(event));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was failing tsc before because of

A spread argument must either have a tuple type or be passed to a rest parameter.

stream.next(new Date());

expect(output).toEqual(expectedResults);
Expand All @@ -46,4 +53,35 @@ describe('operators/ofType', () => {

expect(output).toEqual(expectedResults);
});

// TypeScript TSC test, if this compiles it passes
it('should infer return types of similar unions unions', (done) => {
stream.pipe(ofType(A, B)).subscribe((event) => {
event.keyAOrB;
// @ts-expect-error -- A | B cannot reference a B only key
event.keyBOnly;
done();
});

stream.next(new A());
});

// TypeScript TSC test, if this compiles it passes
it('should infer return types of disparate unions', (done) => {
stream.pipe(ofType(A, C)).subscribe((event) => {
// @ts-expect-error -- A | C cannot reference a key that is not on C
event.keyAOnly;

if (event instanceof A) {
event.keyAOnly;
}

if (event instanceof C) {
event.keyCOptional;
}
done();
});

stream.next(new A());
});
});
10 changes: 6 additions & 4 deletions src/operators/of-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import { IEvent } from '../interfaces';
*
* @return A stream only emitting the filtered instances.
*/
export function ofType<TInput extends IEvent, TOutput extends IEvent>(
...types: Type<TOutput>[]
export function ofType<TInput extends IEvent, T extends Type<IEvent>[]>(
...types: T
) {
const isInstanceOf = (event: IEvent): event is TOutput =>
type InstanceOf<T> = T extends Type<infer I> ? I : never;
const isInstanceOf = (event: IEvent): event is InstanceOf<T[number]> =>
!!types.find((classType) => event instanceof classType);
return (source: Observable<TInput>): Observable<TOutput> =>

return (source: Observable<TInput>): Observable<InstanceOf<T[number]>> =>
source.pipe(filter(isInstanceOf));
}