Skip to content
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

Union type overload inference don't match if types are in different definitions #47480

Closed
kzarist opened this issue Jan 17, 2022 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@kzarist
Copy link

kzarist commented Jan 17, 2022

Bug Report

πŸ”Ž Search Terms

overload inference union type

πŸ•— Version & Regression Information

4.5.4

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about it.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

// library function definition

function libFunction(id: string | number): void;
function libFunction(conditions: {[key: string]: string}): void;
function libFunction(idOrConditions: string | number | {[key: string]: string}): void {
    console.log(idOrConditions);
}

// working example that matches a single definition
function myWrapper(id: string | number){
    libFunction(id);
}

myWrapper("something");
myWrapper(23)


// working example casting the param to any
// That's what I want to avoid
// Our code base is strict and using `any` obriglate
// me to use `eslint-disable` everywhere 
function myWrapperAny(idOrConditions: string | {[key: string]: string}){
    // eslint-disable-next-line
    libFunction(idOrConditions as any);
}

myWrapperAny("something");
myWrapperAny({name: "John"});

// non working wrapper
// Even if there are overloads from both types
// This just don't compile 
function myWrapper2(idOrConditions: string | {[key: string]: string}){
    libFunction(idOrConditions);
}

myWrapper2("something");
myWrapper2({name: "John"});

πŸ™ Actual behavior

No overload signature matches and code don't compile

πŸ™‚ Expected behavior

Overload signature matches and code compile

@whzx5byb
Copy link

Duplicate of #14107

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 18, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants