Why does TypeScript not infer types for some piped functions? Why does TypeScript not infer types for some piped functions? typescript typescript

Why does TypeScript not infer types for some piped functions?


This issue is still outstanding, but for now I wanted to post a workaround that I've come up with because I found it very useful in my experience.

Create a utility function like this:

export const call = <A, B>(f: (x: A) => B) => f;

(the same function as in the question, an identity function that takes a unary function as argument), and then whenever you run into trouble with an arrow function in your pipe, try enclosing it with this utility function. Counterintuitively, TS can't handle this:

const a = pipe(1, x => x + 1, call(x => x + 1));

but it can handle this:

const c = pipe(1, call(x => x + 1), call(x => x + 1));

(no idea why). Later when the issue is fixed (go ahead an upvote it: https://github.com/Microsoft/TypeScript/issues/22081), you'll be able to easily find all references to call and remove them.