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

Type Array.includes as a type guard #203

Open
Danoha opened this issue Aug 27, 2024 · 2 comments
Open

Type Array.includes as a type guard #203

Danoha opened this issue Aug 27, 2024 · 2 comments

Comments

@Danoha
Copy link

Danoha commented Aug 27, 2024

When checking that a variable is one of possible values, my team uses the following:

const status: 400 | 404 | 500;

if ([400, 404].includes(status)) {
  // status should be 400 | 404
}

Maybe we can add a type guard override for Array.includes such that when the second argument (fromIndex) is not specified, it tests that searchElement is one of the array values.

Something like:

interface ReadonlyArray<T> {
    includes(searchElement: unknown): searchElement is T;
    // ... default type
}

Is this feasible?

@Danoha Danoha changed the title Type Array.filter as a type guard Type Array.includes as a type guard Aug 27, 2024
@victorgarciaesgi
Copy link

I dig this too! I though it was by default because otherwise it's not useful at all

@victorgarciaesgi
Copy link

Humm, answer here :( #130

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants