Overload gets lost in mapped type with conditional type
See original GitHub issueTypeScript Version: 3.4.0-dev.201xxxxx
Search Terms: mapped type overload
Code
interface Overloads {
foo(a: string): void;
foo(a: number, b: string): void;
}
/** Converts all properties of an object to Promises and all methods to return Promises */
type ProxiedObject<T> = {
[P in keyof T]: T[P] extends (...args: infer Arguments) => infer R
? (...args: Arguments) => Promise<R>
: Promise<T[P]>
};
declare let x: ProxiedObject<Overloads>;
x.foo("abc"); // Error: [ts] Expected 2 arguments, but got 1. [2554]
x.foo(123, "abc");
Expected behavior: No error, overload should be maintained.
This makes it impossible to use this pattern with popular types that contain overloads, like Rx Observable
pipe()
/subscribe()
.
The ProxiedObject
type is used in https://github.com/GoogleChromeLabs/comlink.
Actual behavior: Overload gets lost, compile error when trying to call the first overload.
Playground Link: link
Issue Analytics
- State:
- Created 5 years ago
- Reactions:28
- Comments:12 (3 by maintainers)
Top Results From Across the Web
Create a mapped type from an interface with overloaded ...
I want to create a mapped type when applied to the interface above ... from the original type, which just gets me back...
Read more >Conditional types in TypeScript - Artsy Engineering
Overloading involves providing multiple type signatures for a single function, ... This extends keyword is the heart of a conditional type.
Read more >What Are Function Overloads in TypeScript?
An error occurs when an implementation signature function is called with an argument of the type corresponding to the implementation signature.ย ...
Read more >Documentation - Advanced Types - TypeScript
If the mapped type is not homomorphic you'll have to give an explicit type parameter to your unwrapping function. Conditional Types. A conditional...
Read more >3. Data model โ Python 3.11.1 documentation
Objects, values and types: Objects are Python's abstraction for data. ... provide additional examples of mapping types, as does the collections module.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Not sure if itโs the same issue but I came here to report this behavior:
Playground
_Related use case: https://github.com/probot/probot/pull/858_
But that was before we had tuple types. This overload:
is equivalent to
which works correctly. I would use that workaround, but I donโt have control over libraries that use overloads. I would at least expect the compiler to treat the above interface equivalently to the below.
If the return type is different, itโs also possible with conditional types:
So I donโt see a reason why overloads would still be impossible for the compiler to handle