type FindCallback<T> = (value: T) => boolean;
type FindResult<T> = (arr: T[]) => T |?undefined;
type FindFn = <T>(callback: FindCallback<T>) => FindResult<T>;
const find: FindFn = (callback) => {
return (arr) => {
for (let idx = 0; idx < arr.length; idx++) {
if (callback(arr[idx]))?{
return arr[idx];
}
}
return undefined;
};
};
const myArray = [1, 5, 4, 9];
const result0 = find<number>((value) => value > 1)(myArray); // works, but explicitly defined the type 'number' in find<number>
const result1 = find((value: number) => value > 1)(myArray); // works, but explicitly defined the type 'number' in the callback (value: number)
const result2 = find((value) => value > 1)(myArray); // my desired way of calling find(), but the callback parameter 'value' and 'result2' are both 'unknown'
// ^
// Object is of type 'unknown'.
I'm trying to improve my understanding of Typescript and functional programming and stumbled upon the following scenario:
I have this higher order find
function that is supposed to find the first element within an array that satisfies a certain condition.
My question now is the following:
Is it possible to improve my typings so that the generic type T
that I used in FindCallback can be inferred from the type of the values in myArray
without explicitly defining it as number
? Also the returned value of find()()
should have either the same type as the elements in the array or undefined
if no element was found.
Here's a link to TS Playground.
question from:
https://stackoverflow.com/questions/65946191/typescript-how-to-infer-a-generic-type-in-a-higher-order-function-from-the-inpu 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…