-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Make "(["foo"] as const).includes()" have a less narrow parameter type #38447
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
Duplicate of #36352. Search terms: |
@MartinJohns I think my suggestion has a difference because it also suggests that if I read the duplicate issue link, and I now understand why allowing |
And that's a duplicate of #36275. |
@MartinJohns I see. I guess I'm confused as to why the change is complicated, it seems to be a small change in the type signature of interface Array<T> {
includes(searchElement: T, fromIndex?: number): searchElement is T;
} |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Search Terms
"as const", "includes", "array includes"
Suggestion
Right now, the type of the first parameter (
searchElement
) ofis "foo" | "bar". However, this isn't very helpful. Because if we can only put in strings that are either "foo" or "bar" into the
includes
method, then there would be no point using it all (it would always returntrue
). As such, I suggest widening the type tounknown
.What
includes
is helpful for is testing if an arbitrary string, "baz" is inside the array["foo", "bar"]
. If it is, then we know it belongs toExampleType
.It would make more sense for the type of
types.includes
to be (rough idea):Use Cases
Examples
Here's what happens right now
Checklist
My suggestion meets these guidelines:
includes
more permissive and the type ofexample
a sub-type of what it already was inside the if statementThe text was updated successfully, but these errors were encountered: