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

Too narrow typing for Array.prototype.includes #48247

Closed
kulak-at opened this issue Mar 14, 2022 · 5 comments
Closed

Too narrow typing for Array.prototype.includes #48247

kulak-at opened this issue Mar 14, 2022 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@kulak-at
Copy link

kulak-at commented Mar 14, 2022

Bug Report

The type for Array.prototype.includes seems to be too narrow making it hard to use in many use cases. Currently Array<T> would use T as a parameter for searchElement which makes it impractical when used to check against optional types:

const arr = [
    "a",
    "b",
    "c",
];

const fn = (val: string | number): val is string => {
    return arr.includes(val);
}

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:

const arr = [
    "a",
    "b",
    "c"
] as const;

const isInConstArray = (val: string): boolean => {
    return arr.includes(val); // Error, Argument of type 'string' is not assignable to parameter of type '"a" | "b" | "c"'.
}

🔎 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.

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Array / includes

⏯ Playground Link

Playground link with relevant code

💻 Code

const arr = [
    "a",
    "b",
    "c"
];

const isInConstArray = (val: string | number): boolean => {
    return arr.includes(val);
}

🙁 Actual behavior

Error when passing val as parameter to arr.includes.

🙂 Expected behavior

arr.includes should accept wider type and behave like type guard instead.

@MartinJohns
Copy link
Contributor

Duplicate of #26255 / #14520.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 14, 2022
@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.

@whzx5byb
Copy link

whzx5byb commented Apr 3, 2022

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

@amerllica
Copy link

amerllica commented Apr 23, 2022

Why has the @typescript-bot closed this issue? This issue is still being happened.

@MartinJohns
Copy link
Contributor

@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
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

6 participants