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(types): add Boolean signature to filter #4961

Merged
merged 2 commits into from Aug 26, 2019
Merged
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
23 changes: 21 additions & 2 deletions spec-dtslint/operators/filter-spec.ts
@@ -1,5 +1,5 @@
import { of } from 'rxjs';
import { filter } from 'rxjs/operators';
import { Observable, of } from 'rxjs';
import { filter, map } from 'rxjs/operators';

it('should support a predicate', () => {
const o = of(1, 2, 3).pipe(filter(value => value < 3)); // $ExpectType Observable<number>
Expand Down Expand Up @@ -38,3 +38,22 @@ 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>
});

// I've not been able to effect a failing dtslint test for this situation and a
// conventional test won't fail because the TypeScript configuration isn't
// sufficiently strict:
// 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;
}

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