-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Too narrow typing for Array.prototype.includes #48247
Labels
Duplicate
An existing issue was already created
Comments
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Workaround: interface ReadonlyArray<T> {
includes<U>(this: T extends U ? this : never, searchElement: U): boolean;
}
const arr = [
"a",
"b",
"c"
] as const;
declare var v1: string;
declare var v2: number;
arr.includes(v1); // OK
arr.includes(v2); // Error as expected |
Why has the @typescript-bot closed this issue? This issue is still being happened. |
@amerllica The bot has closed the issue because it is a duplicate of #26255 / #14520. Feel free to subscribe to the open issue #14520. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bug Report
The type for
Array.prototype.includes
seems to be too narrow making it hard to use in many use cases. CurrentlyArray<T>
would useT
as a parameter for searchElement which makes it impractical when used to check against optional types:The code above should work properly but instead val is too wide typing to be passed to it.
The issue is even more prevalent when used with
as const
keyword:🔎 Search Terms
🕗 Version & Regression Information
Seems to be old bug, I could reproduce it in 3.9.7 which seems to be the first to understand
.includes
syntax on the Playground.⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
Error when passing
val
as parameter toarr.includes
.🙂 Expected behavior
arr.includes
should accept wider type and behave like type guard instead.The text was updated successfully, but these errors were encountered: