Skip to content
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 incorrectly uses generic type #53275

Closed
willyboy opened this issue Mar 15, 2023 · 3 comments
Closed

Array Includes incorrectly uses generic type #53275

willyboy opened this issue Mar 15, 2023 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@willyboy
Copy link

Bug Report

Array.includes should not be inferring type. Especially when the type is a string and the array is defined by a union of strings.
The point of using includes is to see if something exists in an array. If the thing we are looking for is a string in an array with a union type of strings, we get an error like, "argument of type string is not assignable to parameter of type 'a' | 'b'".

🔎 Search Terms

array, includes

🕗 Version & Regression Information

I'm on 4.5.5

⏯ Playground Link

Playground link with relevant code

💻 Code

let thing: string = 'random string that is not hardcoded';
const allowedStrings: ('a' | 'b') = ['a', 'b']

if (!allowedStrings.includes(thing)) {
    // do something
}

🙁 Actual behavior

An error is thrown preventing us from using includes to see if our string is in the array.

🙂 Expected behavior

No error is thrown as we don't know if the string is in the array or not until at runtime.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 15, 2023
@RyanCavanaugh
Copy link
Member

Lacking something like #14520, this is the intended behavior.

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@willyboy
Copy link
Author

willyboy commented Mar 19, 2023

@RyanCavanaugh How is this the intended behavior? "Includes" is basically runtime type checking. It shouldn't even be inferring a type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants