#Copy-paste of EcmaScript's Array.find interface does not work

4 messages · Page 1 of 1 (latest)

minor tulip
#

Hey, I'm trying to create a MyArray class with exactly the same implementation that Array has in ES, but they don't work the same for the case of the find method.

Repro:

cedar nightBOT
#
german.jablo#0

Preview:```ts
// This works as expected:
function isHelloWorld(value: unknown): value is "Hello World" {
return value === "Hello World";
}

const array2 = new Array<number | string>();
const helloWorld_2A = array2.find(isHelloWorld); // helloWorld: "Hello World" | undefined
...```

minor tulip
#

Oh! Is it that I have to repeat the most generic overload (the second one) one more time to establish it as the base implementation?

   find<S extends T>(predicate: (value: T, index: number, obj: T[]) => value is S, thisArg?: any): S | undefined;
   find(predicate: (value: T, index: number, obj: T[]) => unknown, thisArg?: any): T | undefined;

   // The actual implementation of the find method
   find(predicate: (value: T, index: number, obj: T[]) => unknown, thisArg?: any): T | undefined
gilded onyx
#

Yep, the implantation signature is purely for typechecking the implantation, it isn't visible to consumers