I have been working on creating a versatile "pipe" function that takes a series of map functions to transform an input and then output the result of the final map function. Initially, I only implemented a basic version that accepts one map function as a parameter:
const pipe = <T, U>(map: (input: T) => U) => (initial: T): U => map(initial);
However, when I tested it with an identity function, it returned unknown
:
// test is unknown
const test = pipe(i => i)(1);
In this scenario, ideally, test
should be of type number
.
My theory is that pipe(i => i)
is interpreted as pipe(unknown => unknown)
, and no inference is made from the returned function. By calling pipe(unknown => unknown)(1)
, it's acceptable to pass a number into a function that expects unknown
. However, since the function also returns unknown
, that becomes the final result.
I am curious if my hypothesis is accurate and whether there are any discussions about it within the TypeScript development community.
Is there currently a solution in TypeScript that can achieve what I'm aiming for?