zecik123 zecik123 - 3 months ago 31
TypeScript Question

find() and findIndex() in lib.es6.d.ts

Is there a reason why Array.prototype.findIndex() definition doesn't include index and object whereas Array.prototype.find() does?

find(predicate: (value: T, index: number, obj: Array<T>) => boolean, thisArg?: any): T;

findIndex(predicate: (value: T) => boolean, thisArg?: any): number;

I keep getting this error:

Argument of type '(v: number, i: any, a: any) => boolean' is not assignable to parameter of type '(value: number) => boolean'.
(parameter) v: number

when I'm using it like this:

arr.findIndex( (v, i, a) => {/*..*/} )

Am I supposed to edit the .d.ts file?


This seems like a "bug" in the standard definition. According to the spec, it takes the standard Array predicate function: https://tc39.github.io/ecma262/#sec-array.prototype.findindex

I would create a bug report on typescript's github and locally overload findIndex with the right signature.