-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Overloaded function args not inferred correctly #54539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The logic that's going wrong here is that we fetch the contextual type to give to function a1(arg1: unknown): 1;
function a1(arg1: unknown, arg2: unknown): 2;
function a1(...args: ([arg1: unknown] | [arg1: unknown, arg2: unknown])): 1 | 2 {
return args.length;
}
const b1: typeof a1 = function f1(...args) {
return a1(...args);
}
const b2: typeof a1 = function f1(arg1, arg2?) {
return 1;
} I believe we just need to special case rest args to handle their contextual type a little bit differently |
Happens for me aswell |
Weirdly enough, the reported above is from the playground but locally the same code with Node 18 and TS 5.1 will report
|
In the mean time, is there a workaround to get this kind of overloading functionality without triggering this bug? |
Minimal reproduction typescriptlang.org/play?# interface Options {}
interface Props {
action: {
(): void
(newOptions: Partial<Options>): Options
}
}
declare const x: Props
const a = x.action({}) // correctly: Options
const b = x.action() // correctly: void
x.action = (newOptions) => { // Error: Target signature provides too few arguments. Expected 1 or more, but got 0.(2322)
if (newOptions) {
return newOptions
}
return
} Since the type inference works properly outside of the implementation, I guess I will resort to this in the meantime. // @ts-expect-error Target signature provides too few arguments. Expected 1 or more, but got 0.
x.action = (newOptions) => {
... |
Hi, interface a1 {
(arg1: unknown, arg2?: never): 1;
(arg1: unknown, arg2: unknown): 2;
} Note: In the example, we still have an issue: |
May I know how function xx(a: string): void;
function xx(a: string, b: boolean, c?: string): void;
function xx(a: string, b = true, c?: string): void {
console.log(a, b, c)
}
const xxx: typeof xx = function xxx(a: string, b = true, c?: string): void { // b is unknown here
xx(a, b, c); // Argument of type 'unknown' is not assignable to parameter of type 'boolean'.
}; And if I turn off |
Bug Report
π Search Terms
overload functions
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
even simpler is using an interface
π Actual behavior
Error:
Target signature provides too few arguments. Expected 2 or more, but got one.
, given the single signature ofb1
above is variadic, and in fact matches the implementation signature ofa1
, one would assume the two functions are equivalent (from a signature perspective)π Expected behavior
The type of
args
inb1
should be inferred as the union of all overloads ofa1
since it is the only way the variadicargs
would satisfy the assignedtypeof a1
requirement.The text was updated successfully, but these errors were encountered: