Skip to content

typescript throws error when you want to initialize an arrow function that returns different objects based on overloaded string literal union type #55606

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

Closed
reza-ss opened this issue Sep 1, 2023 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@reza-ss
Copy link

reza-ss commented Sep 1, 2023

πŸ”Ž Search Terms

arrow function
union
overload

πŸ•— Version & Regression Information

all TS versions

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.2.2#code/PTAEDsHsCcFsEMA2oBmBXcBjALgS0uKJAG4Cm0ik8AJqAO4wDWAzvaYogIQBQ36WeAqGylm2ABTEAXACIAFu0oyAlFIDeCjpCng0sAEbkAvgG4+GHPkIixk2XNwr1DqfsiREpeOFO9zAq2FRbFA7UDFoXHAAc2U1blBQXBRJUABeDNB5RUgVeMTE6FJsNGhCDRypAEYAJgBmX0SjdmZSUCKSstANXClsaDRSI25hkHbi0vAAFQBPAAc23FZMGCKcTjMbCWytFTNuMew5aEg6VnIT6CTCeGgTulQLQXAebmx5tpJyShp07oSCnYdkpVBUtDo9IZoI1AdJ5I5QS43B4vD4zE0AQCVuAxEExDUpF8KFRaGlQsRlGkAHz5JIpYjpTLA3JxAGFCZdMGUar1GHNRCtcadcoufqDYYYg5gDqTWYLJLLVakdbovHYGriZl7IA

πŸ’» Code

// normal function overload works well!

function test(v:"hello"):{hello:number};
function test(v:"hi"):{hi:boolean};


function test (v: string){
  if(v === "hello"){
    return {hello:123};
  }else return {hi:true}
}
// returnType is correct!;
test("hello");

// throws error in arrow function!

type overload = {
    (v:"hello"):{hello:number};
    (v:"hi"):{hi:boolean};
  }
  
  const test2:overload = (v)=>{
  if(v === "hello"){
    return {hello:123};
  }else return {hi:true}
  }

// returnType is correct!;
  test2("hello");

πŸ™ Actual behavior

typescript throws error when you want to initialize an arrow function that returns different objects based on overloaded string literal union type.

πŸ™‚ Expected behavior

TS most not throws error in this case as it does not throws error in normal function

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

Duplicate of #47669. Currently arrow functions do not support overloading.

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Sep 1, 2023
@typescript-bot
Copy link
Collaborator

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

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 4, 2023
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