-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Narrowed variable types become widened in functions defined after narrowing #50580
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
This is by design, see #9998 and the template "Types not correct in/with callbacks" when creating a new issue. The basic reason is that the narrowing could have become invalid by the time the function gets invoked. |
Ahh that makes sense. The behavior still seems inconsistent with parameters and variables in arrow functions. Why is |
Indeed, not sure why it behaves differently for parameters and variables. |
For Since function declarations get hoisted, we defer checking them, so I think it may just be challenging to figure out where to put the body of |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
🔎 Search Terms
possibly undefined array methods predicate mutable variables narrowing
🕗 Version & Regression Information
4.5.4
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
The variables are not narrowed when used in functions.
param
is narrowed when using an arrow function. This causes friction when trying to use an array method that takes a function as an argument🙂 Expected behavior
Both variables are narrowed to number in all cases
The text was updated successfully, but these errors were encountered: