Open
Description
TypeScript 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