-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Array#includes argument type is too narrow #36352
Comments
Duplicate of #26255 |
Seems like this keeps coming up because there are common idioms which cannot be expressed without unsafe casts. I hear the argument that TS should prevent stupid errors, at the same time it should also try to support idiomatic code naturally. I would argue that until bivariant argument types are supported, it would be better to give this a less restrictive type which supports all the standard use cases, rather than encourage casting. Compare Java's |
Relevant SO answer about this |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
TypeScript Version: my project uses
3.5.3
but I can see the issue is still present in themaster
branch.Search Terms: Array includes
Code
Expected behavior:
It should compile. Specifically, the type definition for
Array#includes
should allow for anysearchElement
type:It is type-safe to ask whether an array contains any value for the same reason it is type-safe to compare any two types with
===
. The example above is a toy example but this pattern shows up all the time in real-world use, e.g. to check if astring
is a member of a union of strings.Actual behavior:
Argument of type 'string' is not assignable to parameter of type '"a"'
Related Issues: None found
The text was updated successfully, but these errors were encountered: