Skip to content

Commit

Permalink
fix(types): add Boolean signature to filter
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Aug 11, 2019
1 parent b0fe286 commit b01ed04
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions spec-dtslint/operators/filter-spec.ts
Expand Up @@ -38,3 +38,9 @@ it('should enforce user-defined type guard types', () => {
const o = of(1, 2, 3).pipe(filter((value: string): value is '1' => value < '3')); // $ExpectError
const p = of(1, 2, 3).pipe(filter((value: number, index): value is 1 => index < '3')); // $ExpectError
});

it('should support Boolean as a predicate', () => {
const o = of(1, 2, 3).pipe(filter(Boolean)); // $ExpectType Observable<number>
const p = of(1, null, undefined).pipe(filter(Boolean)); // $ExpectType Observable<number>
const q = of(null, undefined).pipe(filter(Boolean)); // $ExpectType Observable<never>
});
9 changes: 9 additions & 0 deletions spec/operators/filter-spec.ts
Expand Up @@ -330,4 +330,13 @@ describe('filter operator', () => {

// tslint:disable enable
});

it('should support Boolean as a predicate', () => {
const source = hot('-t--f--^-t-f-t-f--t-f--f--|', { t: 1, f: 0 });
const subs = '^ !';
const expected = '--t---t----t-------|';

expectObservable(source.pipe(filter(Boolean))).toBe(expected, { t: 1, f: 0 });
expectSubscriptions(source.subscriptions).toBe(subs);
});
});
1 change: 1 addition & 0 deletions src/internal/operators/filter.ts
Expand Up @@ -4,6 +4,7 @@ import { Observable } from '../Observable';
import { OperatorFunction, MonoTypeOperatorFunction, TeardownLogic } from '../types';

/* tslint:disable:max-line-length */
export function filter<T>(predicate: BooleanConstructor): OperatorFunction<T, NonNullable<T>>;
export function filter<T, S extends T>(predicate: (value: T, index: number) => value is S,
thisArg?: any): OperatorFunction<T, S>;
export function filter<T>(predicate: (value: T, index: number) => boolean,
Expand Down

0 comments on commit b01ed04

Please sign in to comment.