You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
typeTMethodNames=keyofApi;// GOOD: `parseNumber`typeTMethodImplementations=Api[keyofApi];// GOOD: both signaturestypeTMethodReturns=ReturnType<TMethodImplementations>// BAD: only `number` rather than `number | bigint`
Imagine that you wanted to map over these functions to create a new interface with async versions of each function.
typeAsyncApi={[TMethodNameinkeyofApi]: (...args: Parameters<Api[TMethodName]>)=>Promise<ReturnType<Api[TMethodName]>>};constasyncApi=nullasunknownasAsyncApi;asyncApi.parseNumber('123',false);// GOOD: return type is `Promise<number>`asyncApi.parseNumber('123',true);// BAD: signature is completely missing
🙂 Expected behavior
I would like Typescript to map over each overload.
typeAsyncApi={[TMethodNameinkeyofApi]: (...args: Parameters<Api[TMethodName]>)=>Promise<ReturnType<Api[TMethodName]>>};// I would expect the following type to be produced:// {// parseNumber(input: string, useBigInt: true): Promise<bigint>// parseNumber(input: string, useBigInt: false): Promise<number>// }
The text was updated successfully, but these errors were encountered:
type TMethodReturns = ReturnType // BAD: only number rather than number | bigint
This is a design limitation. ReturnType<T> is a using inference within conditional type, and inference within conditional types do not play well together with overloads.
When inferring from a type with multiple call signatures (such as the type of an overloaded function), inferences are made from the last signature (which, presumably, is the most permissive catch-all case). It is not possible to perform overload resolution based on a list of argument types.
Bug Report
🔎 Search Terms
mapped types, mapping, map, interfaces, overloads, function,
ReturnType
,keyof
🕗 Version & Regression Information
This is the behavior in every version I tried between 4.9.5 and 5.1.0-dev.20230322, and I reviewed the FAQ for entries about ‘overload.’
⏯ Playground Link
Playground link with relevant code
💻 Code
Imagine an interface with two function overloads.
🙁 Actual behavior
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.
Imagine that you wanted to map over these functions to create a new interface with async versions of each function.
🙂 Expected behavior
I would like Typescript to map over each overload.
The text was updated successfully, but these errors were encountered: