mattpocock/ts-reset

Type `Array.includes` as a type guard

Opened this issue · 2 comments

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?

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

Humm, answer here :( #130