Check array includes a value, type-safe version.
This function provides a type-guard version of Array.includes that properly narrows the type of the element after checking.
The collection to search in
The element to check for
Type guard indicating if the element is in the collection
const colors = ['red', 'green', 'blue'] as constconst value = 'red'if (arrayIncludes(colors, value)) { // value is now typed as 'red' | 'green' | 'blue' console.log(value)} Copy
const colors = ['red', 'green', 'blue'] as constconst value = 'red'if (arrayIncludes(colors, value)) { // value is now typed as 'red' | 'green' | 'blue' console.log(value)}
https://oida.dev/typescript-array-includes/
Check array includes a value, type-safe version.
This function provides a type-guard version of Array.includes that properly narrows the type of the element after checking.