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

Add arrayIncludes() #8

Merged
merged 6 commits into from
Nov 3, 2021
Merged

Conversation

ifiokjr
Copy link
Contributor

@ifiokjr ifiokjr commented Nov 1, 2021

Description

This adds an improved arrayIncludes function which acts as a type guard.

import {arrayIncludes} from 'ts-extras';

const values = ['a', 'b', 'c'] as const;
declare const valueToCheck: unknown;
let value: typeof values[number];

if (arrayIncludes(values, valueToCheck)) {
  // Now we know that the value is of type `typeof values[number]`.
  value = valueToCheck
} else {
  // @ts-expect-error
  value = valueToCheck
}

source/index.ts Outdated Show resolved Hide resolved
test/array-includes.ts Outdated Show resolved Hide resolved
test/array-includes.ts Outdated Show resolved Hide resolved
@sindresorhus sindresorhus merged commit f988c40 into sindresorhus:main Nov 3, 2021
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

Successfully merging this pull request may close these issues.

None yet

2 participants