Skip to content

Commit

Permalink
feat(filter): improve type inference for filter(Boolean)
Browse files Browse the repository at this point in the history
  • Loading branch information
josepot committed Oct 15, 2020
1 parent adbe65e commit ad951b1
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 4 deletions.
2 changes: 1 addition & 1 deletion api_guard/dist/types/operators/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export declare function expand<T, R>(project: (value: T, index: number) => Obser
export declare function expand<T, R>(project: (value: T, index: number) => ObservableInput<R>, concurrent: number | undefined, scheduler: SchedulerLike): OperatorFunction<T, R>;

export declare function filter<T, S extends T>(predicate: (value: T, index: number) => value is S, thisArg?: any): OperatorFunction<T, S>;
export declare function filter<T>(predicate: BooleanConstructor): OperatorFunction<T | null | undefined, NonNullable<T>>;
export declare function filter<T>(predicate: BooleanConstructor): OperatorFunction<T, T extends null | undefined | false | 0 | 0n | '' ? never : T>;
export declare function filter<T>(predicate: (value: T, index: number) => boolean, thisArg?: any): MonoTypeOperatorFunction<T>;

export declare function finalize<T>(callback: () => void): MonoTypeOperatorFunction<T>;
Expand Down
2 changes: 1 addition & 1 deletion spec-dtslint/operators/filter-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ it('should support Boolean as a predicate', () => {
// https://github.com/ReactiveX/rxjs/issues/4959#issuecomment-520629091
it('should support inference from a return type with Boolean as a predicate', () => {
interface I {
a: string | null;
a: string | null | undefined | false | 0 | 0n | '';
}

const i$: Observable<I> = of();
Expand Down
3 changes: 1 addition & 2 deletions src/internal/operators/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { OperatorSubscriber } from './OperatorSubscriber';

/* tslint:disable:max-line-length */
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: BooleanConstructor): OperatorFunction<T, T extends null | undefined | false | 0 | 0n | '' ? never : 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 ad951b1

Please sign in to comment.