Skip to content

Overloaded function args not inferred correctlyΒ #54539

Open
@imatlopez

Description

Bug Report

πŸ”Ž Search Terms

overload functions

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about overloading and different function arguments behavior

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

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);
}

even simpler is using an interface

interface a1 {
    (arg1: unknown): 1;
    (arg1: unknown, arg2: unknown): 2;
}

// errors
const b1: a1 = function f1(...args) {
    return args.length;
}

πŸ™ Actual behavior

Error: Target signature provides too few arguments. Expected 2 or more, but got one., given the single signature of b1 above is variadic, and in fact matches the implementation signature of a1, one would assume the two functions are equivalent (from a signature perspective)

πŸ™‚ Expected behavior

The type of args in b1 should be inferred as the union of all overloads of a1 since it is the only way the variadic args would satisfy the assigned typeof a1 requirement.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Help WantedYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some cases

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions