Skip to content

String union type Array.includes is problematic. #46186

@pouyakary

Description

@pouyakary

Bug Report

🔎 Search Terms

.includes + union

🕗 Version & Regression Information

Version 4.3.5

Playground link with relevant code

💻 Code

type MyType =
    "X" | "Y" | "Z"

const myArray: MyType[ ] = [
    "X", "Y", "Z"
]

let something = "string"

myArray.includes( something )

🙁 Actual behavior

TypeScript gives error on something, while if I knew it existed in the myArray, I wouldn't have used the .includes.

🙂 Expected behavior

My expectation is that I can send whatever string to .includes( ) and when getting back a true be sure that string is of type MyType. Then how else do you check if your string belongs to a union string type than having the code above? So I think the case of .includes( union type) it should accept all the primitive types that array excepts.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions