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

Update filter.ts #5937

Merged
merged 5 commits into from
Jan 26, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion api_guard/dist/types/operators/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ export declare function exhaustMap<T, I, R>(project: (value: T, index: number) =
export declare function expand<T, R>(project: (value: T, index: number) => ObservableInput<R>, concurrent?: number, scheduler?: SchedulerLike): OperatorFunction<T, R>;
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>(predicate: (value: T, index: number) => false, thisArg?: any): OperatorFunction<T, never>;
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, TruthyTypesOf<T>>;
export declare function filter<T>(predicate: (value: T, index: number) => boolean, thisArg?: any): MonoTypeOperatorFunction<T>;
Expand Down
12 changes: 8 additions & 4 deletions spec-dtslint/operators/filter-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ it('should support Boolean as a predicate', () => {
const x = of(false, false, false, false).pipe(filter(Boolean)); // $ExpectType Observable<true>
});

it('should narrow on always-false predicates', () => {
const o = of(1, 2, 3).pipe(filter(() => false)); // $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:
Expand All @@ -79,3 +75,11 @@ it('should support inference from a generic return type of the predicate', () =>

const o$ = of(1, null, {foo: 'bar'}, true, undefined, 'Nick Cage').pipe(filter(isDefined())); // $ExpectType Observable<string | number | boolean | { foo: string; }>
});

it('should support inference from a predicate that returns any', () => {
function isTruthy(value: number): any {
return !!value;
}

const o$ = of(1).pipe(filter(isTruthy)); // $ExpectType Observable<number>
});
1 change: 0 additions & 1 deletion src/internal/operators/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { OperatorFunction, MonoTypeOperatorFunction, TruthyTypesOf } from '../ty
import { operate } from '../util/lift';
import { OperatorSubscriber } from './OperatorSubscriber';

export function filter<T>(predicate: (value: T, index: number) => false, thisArg?: any): OperatorFunction<T, never>;
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: BooleanConstructor): OperatorFunction<T, TruthyTypesOf<T>>;
export function filter<T>(predicate: (value: T, index: number) => boolean, thisArg?: any): MonoTypeOperatorFunction<T>;
Expand Down