ts-extras
Version:
Essential utilities for TypeScript projects
25 lines (19 loc) • 594 B
JavaScript
/**
An alternative to `Set#has()` that properly acts as a type guard.
It was [rejected](https://github.com/microsoft/TypeScript/issues/42641#issuecomment-774168319) from being done in TypeScript itself.
```
import {setHas} from 'ts-extras';
const values = ['a', 'b', 'c'] as const;
const valueSet = new Set(values);
const valueToCheck: unknown = 'a';
if (setHas(valueSet, valueToCheck)) {
// We now know that the value is of type `typeof values[number]`.
}
```
*/
export function setHas(set, item) {
return set.has(item);
}