steveluscher
steveluscher

Reputation: 4228

How to preserve function overloads when mapping over a Typescript interface

Code

Imagine an interface with two function overloads.

interface Api {
  parseNumber(input: string, useBigInt: true): bigint
  parseNumber(input: string, useBigInt: false): number
}

How can you map over these functions to create a new interface with async versions of each function?

{
  parseNumber(input: string, useBigInt: true): Promise<bigint>
  parseNumber(input: string, useBigInt: false): Promise<number>
}

What I tried

You can easily pull out a union of function names, a union of the function signatures, but when you ask for a union of the return types you only get the last one in declaration order.

type TMethodNames = keyof Api; // GOOD: `parseNumber`
type TMethodImplementations = Api[keyof Api]; // GOOD: both signatures
type TMethodReturns = ReturnType<TMethodImplementations> // BAD: only `number` rather than `number | bigint`

Trying to produce a new interface via mapping results in all but the last function signature being dropped.

type AsyncApi = {
  [TMethodName in keyof Api]: (...args: Parameters<Api[TMethodName]>) => Promise<ReturnType<Api[TMethodName]>>
};

const asyncApi = null as unknown as AsyncApi;
asyncApi.parseNumber('123', false); // GOOD: return type is `Promise<number>`
asyncApi.parseNumber('123', true); // BAD: signature is completely missing

Upvotes: 2

Views: 160

Answers (0)

Related Questions