Skip to content

Commit 8255365

Browse files
kolodnybenlesh
authored andcommitted
fix(filter): Fix overload order for filter to support inferring the generic type (#5024)
* fix(filter): Fix overload order for filter to support inferring the generic type. The provided test will create an Observable<never> with the order unchanged. * Expand test to include a robust union type
1 parent bc91ba0 commit 8255365

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

spec-dtslint/operators/filter-spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,14 @@ it('should support inference from a return type with Boolean as a predicate', ()
5656

5757
const i$: Observable<I> = of();
5858
const s$: Observable<string> = i$.pipe(map(i => i.a), filter(Boolean)); // $ExpectType Observable<string>
59-
});
59+
});
60+
61+
it('should support inference from a generic return type of the predicate', () => {
62+
function isDefined<T>() {
63+
return (value: T|undefined|null): value is T => {
64+
return value !== undefined && value !== null;
65+
};
66+
}
67+
68+
const o$ = of(1, null, {foo: 'bar'}, true, undefined, 'Nick Cage').pipe(filter(isDefined())); // $ExpectType Observable<string | number | boolean | { foo: string; }>
69+
});

src/internal/operators/filter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { Observable } from '../Observable';
44
import { OperatorFunction, MonoTypeOperatorFunction, TeardownLogic } from '../types';
55

66
/* tslint:disable:max-line-length */
7-
// NOTE(benlesh): T|null|undefined solves the issue discussed here: https://github.com/ReactiveX/rxjs/issues/4959#issuecomment-520629091
8-
export function filter<T>(predicate: BooleanConstructor): OperatorFunction<T|null|undefined, NonNullable<T>>;
97
export function filter<T, S extends T>(predicate: (value: T, index: number) => value is S,
108
thisArg?: any): OperatorFunction<T, S>;
9+
// NOTE(benlesh): T|null|undefined solves the issue discussed here: https://github.com/ReactiveX/rxjs/issues/4959#issuecomment-520629091
10+
export function filter<T>(predicate: BooleanConstructor): OperatorFunction<T|null|undefined, NonNullable<T>>;
1111
export function filter<T>(predicate: (value: T, index: number) => boolean,
1212
thisArg?: any): MonoTypeOperatorFunction<T>;
1313
/* tslint:enable:max-line-length */

0 commit comments

Comments
 (0)