Type inference in TypeScript for curried function

Werner de Groot

I have the following definition for a curried function in TypeScript:

interface Curried2<A, B, Z> {
    (_0: A): (_0: B) => Z;
    (_0: A, _1: B): Z;
}

I have the following function that should accept a function (curried or not):

function apply<T, U>(f: (_0: T) => U, x: T): U {
    return f(x);
}

Now, given

let curried: Curried2<number, boolean, string> = ...

the following works (as expected):

apply<number, (_0: boolean) => string>(curried, 4);

But TypeScript cannot infer the type on its own:

apply(curried, 4);

(Even though there is only one overload for () which takes a single value.) It complains:

Argument of type 'Curried2<number, boolean, string>' is not assignable to parameter of type '(_0: number) => string' ...
It has correctly inferred T, but inferred U to be string. Why is this? What can I do to make type inference work for me in this case (as explicitly specifying T and U is too verbose for my taste)?

Thanks in advance!

shadeglare

I could try this approach:

type Curried<T1, T2, T3> = (x: T1) => (y: T2) => T3;

function apply<T, U>(f: (value: T) => U, x: T): U {
    return f(x);
}

//simple demo
var someFunc: Curried<string, number, [string, number]> = x => y => [x, y];
var curriedSomeFunc = apply(someFunc, "string here");
var result1 = curriedSomeFunc(0); //will be ["string here", 0]
var result2 = curriedSomeFunc(1); //will be ["string here", 1]

Another try. The syntax is correct but no safety if you pass not a correct function for currying:

interface Curried<T1, T2, T3> {
    (x: T1, y: T2): T3;
    (x: T1): (y: T2) => T3
}

let f1 = <Curried<string, number, [string, number]>>
    ((x: string, y: number) => [x, 1]);
let f2 = <Curried<string, number, [string, number]>>
    ((x: string) => (y: number) => [x, y]);

function apply<T, V>(f: (x: T) => V, x: T): V {
    return f(x);
}

let curriedF1 = apply(f1, "42"); //Won't work (no function to curry) but type inference is OK
let curriedF2 = apply(f2, "11"); //Will work and type inference is also OK 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Function overloading and type inference in typescript

From Dev

F# Type inference with curried functions

From Dev

TypeScript type inference issue

From Dev

TypeScript: Incorrect type inference

From Dev

Typescript union type inference

From Dev

type inference of (>>)(>>) (function composition)

From Dev

Type inference with function literals

From Dev

type inference of (>>)(>>) (function composition)

From Dev

TypeScript generics: argument type inference

From Dev

Typescript: Generic class type inference

From Dev

Can function type be defined by inference?

From Dev

Type inference of generic function parameter

From Dev

function composition type inference in haskell

From Dev

Scala type inference with function types

From Dev

Haskell, polyvariadic function and type inference

From Dev

TypeScript variadic tuple type inference issue, for function that runs a sequence of "Result"-returning functions

From Dev

TypeScript excess field checking and type inference error

From Dev

Typescript: Type inference when using decorator

From Dev

Typescript type inference not recognizing value must be defined

From Dev

Haskell function argument type inference with typeclasses

From Dev

Strange behavior of type inference in function with upper bound

From Dev

Type inference not working as expected for generic function

From Dev

Type inference not working as expected for generic function

From Dev

Type inference not working when passing map function

From Dev

Evaluate curried function

From Dev

Generic curried map function

From Dev

Get arity of curried function

From Dev

scala curried function with yield

From Dev

Curried function in scala

Related Related

HotTag

Archive