-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Callback parameter type is any for union of arrays #52748
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
Note #37453 is nearly the same issue, but for a union of a set and an array. It was closed saying:
I don't claim to understand the details of that limitation, but it seems like it should not apply here since the source declaration is just the array prototype for all members of the union. |
arr1.forEach((val) => {
// val incorrectly inferred as any
console.log(val);
});
|
Sorry, pasted the wrong code. I corrected to match the playground. However, even if I do just correct that empty array version to use |
In the Playground as posted, I'm not sure where you're getting |
Ugh... finally corrected code and playground not to be I don't know how to see the |
FWIW I can reproduce the |
So after some investigation it seems the problem only occurs when |
This is the intended behavior. The |
That's very confusing indeed...
I also don't understand why fixing this would be avoided as a breaking change. If anyone is relying on Please consider altering this intention at least for version 5. |
@steverep AFAIK @RyanCavanaugh What's going on here? |
This was really done on purpose, not sure what else to say. See #42620
This isn't true.
Probably the problem is that you still have |
So all told, this ends up being a duplicate of #47294
The implicit any error goes away after clearing the checkbox, so that's not it. For posterity, it turns out the problem was that toggling |
I agree this is very similar to #47294, but that issue claims things changed between 4.2 and 4.3, which doesn't seem to be the case here. I went back as far as 3.9 and get the same Also, that issue was closed without a suitable resolution IMHO. The author also points to the documentation not being aligned with the actual behavior. It's documented as a "Type Checking" option and says:.
Reading that, there's no way a user should glean that it should be altering inferences. In fact it says the opposite. I guess I don't know what else to say either, except that this should be addressed one way or another. Inferring |
I get the sense that we're all talking past each other.
It's the same issue - prior to 4.3, it always inferred
|
Ah... I missed that subtlety - thanks for explaining. For my edification, what do you mean by "OP"? And I don't want to seem like I'm jumping on or over anyone. I'm just trying to make a good case for this not to be written off as "intended behavior". Certainly not all intentions have only positive consequences. π |
"OP" means Original Post, and/or Original Poster, depending on context. In this case it's referring to the main text of your issue at the top of the page, prior to all the replies. |
@weswigham could you comment on why #42620 only improved type inferences when @RyanCavanaugh is that PR the only case where |
We try not to introduce new errors into existing code, especially if that code has |
Um, isn't that exactly what the PR in question does - strengthens contextual types to expose errors? It doesn't require If this is intentional only in the name of backwards compatibility, it seems to me that #42620 should have left the behavior of Please note also that my actual use case results in a situation where |
Changing an If every change we did had an associated back-compat flag, there'd be thousands of them instead of hundreds. The approach isn't really scalable.
But that's exactly what happened. People with
I don't see a check-time error in either case, so I'm not sure what you're referring to. |
See this playground code instead, which is more aligned with the current use case that prompted me to open the issue. Having
I disagree. I don't see that as leaving the purpose of |
Hmm, this error probably shouldn't happen. |
Yeah, though I'm not 100% sure how to make that work. I think it'd be ridiculous for |
You say that, and yet |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
π Search Terms
array, union, callback, any, contextual
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
The
for..of
loop correctly infers the type, but array methods (e.g.forEach
,map
,filter
, etc.) infer the array item type to beany
.π Expected behavior
Given a type which is the union of arrays and/or tuples,
T1[] | T2[] | ... Tn[]
, array methods which take functions should infer the item parameter's type asT1[number] | T2[number] | ... Tn[number]
.The text was updated successfully, but these errors were encountered: