Skip to content

Commit

Permalink
fix(filter): Fix overload order for filter to support inferring the g…
Browse files Browse the repository at this point in the history
…eneric 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
  • Loading branch information
kolodny authored and benlesh committed Oct 15, 2019
1 parent bc91ba0 commit 8255365
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 11 additions & 1 deletion spec-dtslint/operators/filter-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,14 @@ it('should support inference from a return type with Boolean as a predicate', ()

const i$: Observable<I> = of();
const s$: Observable<string> = i$.pipe(map(i => i.a), filter(Boolean)); // $ExpectType Observable<string>
});
});

it('should support inference from a generic return type of the predicate', () => {
function isDefined<T>() {
return (value: T|undefined|null): value is T => {
return value !== undefined && value !== null;
};
}

const o$ = of(1, null, {foo: 'bar'}, true, undefined, 'Nick Cage').pipe(filter(isDefined())); // $ExpectType Observable<string | number | boolean | { foo: string; }>
});
4 changes: 2 additions & 2 deletions src/internal/operators/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { Observable } from '../Observable';
import { OperatorFunction, MonoTypeOperatorFunction, TeardownLogic } from '../types';

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

0 comments on commit 8255365

Please sign in to comment.