Skip to content

Wrong type when function signature changes #55720

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
NilsIrl opened this issue Sep 12, 2023 · 3 comments
Closed

Wrong type when function signature changes #55720

NilsIrl opened this issue Sep 12, 2023 · 3 comments
Labels
Not a Defect This behavior is one of several equally-correct options

Comments

@NilsIrl
Copy link
Contributor

NilsIrl commented Sep 12, 2023

🔎 Search Terms

  • function signature
  • wrong type
  • changed
  • re-assigned
  • re-declared
  • type inference

🕗 Version & Regression Information

  • This is the behavior in every version (3.3.3333 and 5.2.2) I tried, and I reviewed the FAQ for entries about bugs that aren't bugs.

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.2.2#code/DYUwLgBAzg9gtiAYgVwHYGMIF4IAoCGAXBAPIBGAViOmAJTYB8E+A3AFDoypST7bTwkadLgDeAX1rtYCFBn656WJgFZ2nbpDL8ZQjIpZA

💻 Code

let someFunc = (a: Object) => a;
const a = someFunc({});
someFunc = () => 5;
const b = someFunc();

🙁 Actual behavior

It doesn't compile and there is an error on the second call to someFunc saying that an argument is not supplied.

🙂 Expected behavior

Because someFunc was redefined it doesn't actually expect an argument and the code is correct.

Additional information about the issue

When using the function keyword, the inferred type is correct (although it doesn't compile either but this time because there is a duplicate function implementation):

https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABAZzgWwKYDFwQBQCGAXIgPIBGAVhtAJSIDeiAThlCM0gQNyIC+AKAgJkURAUQBeFOmy48DPrW4DQkWAhmYckPPSat2nRn16DhYUYnJStc3cqA

@MartinJohns
Copy link
Contributor

MartinJohns commented Sep 12, 2023

Possibly a duplicate of #27706, although there's no narrowing happening in this case.

Point 2 and 3 of this comment sound relevant: #36579 (comment)

Variables initialized with some expression always assume the widened form of the initializer's type
Assignment will only create a narrowing to a more-specific type if the declared type of the assigned variable is a union

So it sounds like it's working as intended.

@RyanCavanaugh RyanCavanaugh added the Not a Defect This behavior is one of several equally-correct options label Sep 13, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Not a Defect" 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 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Not a Defect This behavior is one of several equally-correct options
Projects
None yet
Development

No branches or pull requests

4 participants