-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
Description
Bug Report
π Search Terms
TypeScript, Generic, Overload resolution
π Version & Regression Information
Always been there for as far as I can go back on TypeScript playground
β― Playground Link
Playground link with relevant code
π» Code
type ArgsType<T> = T extends (...args: infer A) => any ? A : never
type FunctionProperty<T> = {
[K in keyof T]: T[K] extends IsCallable<T[K]> ? K : never
}[keyof T]
type IsCallable<T> = T extends (...args: any[]) => any ? T : never
function Method<T extends {},
M extends FunctionProperty<T>,
A extends ArgsType<Required<T>[M]>,
>(inst: T, method: M, ...args: A): ReturnType<Required<T>[M]> {
return inst[method](...args);
}
class Drink {
public Wine(): Promise<string>
public Wine(callback: ((result: string) => void)): void
public Wine(callback?: ((result: string) => void)): void | Promise<string> {
if (callback) {
return callback("Wine");
}
return Promise.resolve("Wine");
}
}
(async function () {
const drink = new Drink()
// note how the `Method` function only considers the function's implementation
// we can fix this by making `callback` in the second overload optional, but
// that means that we would be able to call the `void` method without a `callback`.
// not what we want either.
console.log(await Method(drink, "Wine"))
Method(drink, "Wine", (res: string) => void console.log(res))
})().then(() => console.log("Done"));
π Actual behavior
Only the last overload's signature is considered.
π Expected behavior
Correct overload being chosen, respecting both arguments (which is where it fails now) and next up based on returntype.
While this is a contrived example to write out in TS, it's much less so when working with external libraries that suffer from this where only the last overload is considered.
reduckted
Metadata
Metadata
Assignees
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed