Typescript: How to infer a generic type in a higher order function from the input parameter of the returned function

Sascha
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.

Linda Paiste

If this were to be a function with two arguments: callback and array then it would be simple. As it is, you have two separate functions. You cannot infer the type of the first function based on the arguments that you pass to the second function.

This higher-order function structure means that the returned FindResult function does not need to be immediately invoked. What is the type of const mapper = find((value) => true)? It's a function that gets called on an array of ...? Without annotating value, you simply cannot know what type of array it will eventually be called with.

Inference based on the type of the array is only possible when the array is an argument of the function.

type FindFn = <T>(callback: FindCallback<T>, arr: T[]) => T | undefined;

const find: FindFn = (callback, arr) => { ...

Playground Link

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

infer type parameter from function argument

From Dev

How to access to the parameter of the higher order function in scala

From Dev

Infer function generic type U from return value of passed function

From Dev

How to constrain the type of generic parameter to a Typescript function to be an enum

From Dev

Incompatible flow-type for generic higher order function

From Dev

Can typescript infer a function's response type based on parameter values?

From Java

TypeScript function return type based on input parameter

From Java

Dotty cannot infer result type of generic Scala function taking type parameter trait with abstract type

From Dev

Call static function of Generic Type from Generic Type in typescript

From Dev

How to specify a generic type for a function parameter

From Dev

Type definitions and type mismatch in a higher order function

From Dev

TypeScript - Extend type returned by a function

From Dev

TypeScript return exact function infer type

From Dev

Why TypeScript can not infer type of recursive function

From Dev

TypeScript: How to type a returned function's return type

From Dev

.Net Reflection calling a function with a generic parameter - How to pass the type to the function

From Dev

Scala: Polymorphism in argument type of higher order function

From Dev

Higher order function, Flow type annotations

From Dev

Getting a generic method to infer the type parameter from the runtime type

From Dev

How to infer class from generic type in Java?

From Dev

Type inference of generic function parameter

From Dev

Generic return type of function in Typescript

From Dev

Higher-Order Function Scheme: Incrementing via Returned Value

From Dev

Difference between generic function and function with Type parameter

From Dev

Why can't C# compiler infer generic-type delegate from function signature?

From Dev

Infer generic class type parameters from single constructor parameter

From Dev

Function from a higher order applied to an array

From Dev

Returning an array from a higher order function

From Dev

TypeScript: Infer type of object of generic interface implicitly from object content

Related Related

  1. 1

    infer type parameter from function argument

  2. 2

    How to access to the parameter of the higher order function in scala

  3. 3

    Infer function generic type U from return value of passed function

  4. 4

    How to constrain the type of generic parameter to a Typescript function to be an enum

  5. 5

    Incompatible flow-type for generic higher order function

  6. 6

    Can typescript infer a function's response type based on parameter values?

  7. 7

    TypeScript function return type based on input parameter

  8. 8

    Dotty cannot infer result type of generic Scala function taking type parameter trait with abstract type

  9. 9

    Call static function of Generic Type from Generic Type in typescript

  10. 10

    How to specify a generic type for a function parameter

  11. 11

    Type definitions and type mismatch in a higher order function

  12. 12

    TypeScript - Extend type returned by a function

  13. 13

    TypeScript return exact function infer type

  14. 14

    Why TypeScript can not infer type of recursive function

  15. 15

    TypeScript: How to type a returned function's return type

  16. 16

    .Net Reflection calling a function with a generic parameter - How to pass the type to the function

  17. 17

    Scala: Polymorphism in argument type of higher order function

  18. 18

    Higher order function, Flow type annotations

  19. 19

    Getting a generic method to infer the type parameter from the runtime type

  20. 20

    How to infer class from generic type in Java?

  21. 21

    Type inference of generic function parameter

  22. 22

    Generic return type of function in Typescript

  23. 23

    Higher-Order Function Scheme: Incrementing via Returned Value

  24. 24

    Difference between generic function and function with Type parameter

  25. 25

    Why can't C# compiler infer generic-type delegate from function signature?

  26. 26

    Infer generic class type parameters from single constructor parameter

  27. 27

    Function from a higher order applied to an array

  28. 28

    Returning an array from a higher order function

  29. 29

    TypeScript: Infer type of object of generic interface implicitly from object content

HotTag

Archive