Skip to content

Commit

Permalink
Update filter.ts (#5937)
Browse files Browse the repository at this point in the history
* Update filter.ts

Remove helpful type at the beginning because it actually was incorrectly returning type never in some cases.

* Update filter-spec.ts

Remove filter test

* test: add test that fails if sig not removed

* test: change param type annotation

* chore: update golden files

Co-authored-by: Nicholas Jamieson <nicholas@cartant.com>
Co-authored-by: Ben Lesh <ben@benlesh.com>
  • Loading branch information
3 people authored Jan 26, 2021
1 parent c917c52 commit 2c89bb7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
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

0 comments on commit 2c89bb7

Please sign in to comment.