I'm recently exploring TypeScript again. One of it's key limitations seems to be the incapability of typing function composition. Let me first show you the JavaScript code. I'm trying to type this:
const getUserById = id => new Promise((resolve, reject) => id === 1
? resolve({ id, displayName: 'Jan' })
: reject('User not found.')
);
const getName = ({ displayName }) => displayName;
const countLetters = str => str.length;
const asyncIsEven = n => Promise.resolve(n % 2 === 0);
const asyncPipe = (...fns) => x => fns.reduce(async (y, f) => f(await y), x);
const userHasEvenName = asyncPipe(
getUserById,
getName,
countLetters,
asyncIsEven
);
userHasEvenName(1).then(console.log);
// β³ false
userHasEvenName(2).catch(console.log);
// β³ 'User not found.'
Here asyncPipe
composes regular functions as well as promises in anti-mathematical order (from left to right). I would love to write an asyncPipe
in TypeScript, that knows about the input and output types. So userHasEvenName
should know, that it takes in a number and returns a Promise<boolean>
. Or, if you comment out getUserById
and asyncIsEven
it should know that it takes in a User
and returns a number.
Here are the helper functions in TypeScript:
interface User {
id: number;
displayName: string;
}
const getUserById = (id: number) => new Promise<User>((resolve, reject) => id === 1
? resolve({ id, displayName: 'Jan' })
: reject('User not found.')
);
const getName = ({ displayName }: { displayName: string }) => displayName;
const countLetters = (str: string) => str.length;
const asyncIsEven = (n: number) => Promise.resolve(n % 2 === 0);
I would love to show you all my approaches for asyncPipe
but most were way off. I found out that in order to write a compose
function in TypeScript, you have to heavily overload it because TypeScript can't handle backwards inference and compose
runs in mathematical order. Since asyncPipe
composes from left to right, it feels like it's possible to write it. I was able to explicitly write a pipe2
that can compose two regular functions:
function pipe2<A, B, C>(f: (arg: A) => B, g: (arg: B) => C): (arg: A) => C {
return x => g(f(x));
}
How would you write asyncPipe
that asynchronously composes an arbitrary amount of function or promises and correctly infers the return type?
(x: any)
andFunction[]
, which both constitute blanks in the resulting types. I don't know Typescript much, but this seems to be hacking on the type level. β Nancynandor