Skip to content

Commit

Permalink
fix: predicates that return any will now behave property with findI…
Browse files Browse the repository at this point in the history
…ndex (#6097)

* test: add failing test for findIndex

* fix: remove incompatible-with-any findIndex sig

* chore: update api_guardian

* test: remove now-irrelevant test
  • Loading branch information
cartant authored Mar 8, 2021
1 parent 868e409 commit c6f73d6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 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 @@ -124,7 +124,6 @@ export declare function find<T>(predicate: BooleanConstructor): OperatorFunction
export declare function find<T, S extends T>(predicate: (value: T, index: number, source: Observable<T>) => value is S, thisArg?: any): OperatorFunction<T, S | undefined>;
export declare function find<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean, thisArg?: any): OperatorFunction<T, T | undefined>;

export declare function findIndex<T>(predicate: (value: T, index: number, source: Observable<T>) => false, thisArg?: any): OperatorFunction<T, -1>;
export declare function findIndex<T>(predicate: BooleanConstructor, thisArg?: any): OperatorFunction<T, T extends Falsy ? -1 : number>;
export declare function findIndex<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean, thisArg?: any): OperatorFunction<T, number>;

Expand Down
9 changes: 6 additions & 3 deletions spec-dtslint/operators/findIndex-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ it('should support Boolean constructor', () => {
const b = of(0 as const, -0 as const, null, 'hi there' as const, undefined, false as const, '' as const).pipe(findIndex(Boolean)); // $ExpectType Observable<number>
});

it('should properly narrow an always false predicate', () => {
const a = of('foo', 'bar', 'baz').pipe(findIndex(() => false)); // $ExpectType Observable<-1>
})
it('should support inference from a predicate that returns any', () => {
function isTruthy(value: number): any {
return !!value;
}
const a = of(1).pipe(findIndex(isTruthy)); // $ExpectType Observable<number>
});
1 change: 0 additions & 1 deletion src/internal/operators/findIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Falsy, OperatorFunction } from '../types';
import { operate } from '../util/lift';
import { createFind } from './find';

export function findIndex<T>(predicate: (value: T, index: number, source: Observable<T>) => false, thisArg?: any): OperatorFunction<T, -1>;
export function findIndex<T>(predicate: BooleanConstructor, thisArg?: any): OperatorFunction<T, T extends Falsy ? -1 : number>;
export function findIndex<T>(
predicate: (value: T, index: number, source: Observable<T>) => boolean,
Expand Down

0 comments on commit c6f73d6

Please sign in to comment.