Skip to content

makeStringUnionGuard

Jason Godesky edited this page Jul 11, 2026 · 2 revisions

makeStringUnionGuard is a method that returns a type guard that asserts that the candidate is a member of a given string union (see makeStringUnion).

Parameters

values: readonly T[]

The values of your string union.

Returns

A type guard that asserts that the candidate passed to it is a member of the string union.

Examples

import { makeStringUnion, makeStringUnionGuard } from '@revolutionarygamesco/common'

const testTypes = ['a', 'b', 'c'] as const
type TestType = typeof testTypes[number]
const isTestType = makeStringUnionGuard<TestType>(testTypes)

isTestType('a') // true
isTestType('b') // true
isTestType('c') // true
isTestType('d') // false
isTestType('hello!') // false
isTestType(42) // false
isTestType(testTypes) // false because that's an array — `TestType[]`, not `TestType`

See also

Clone this wiki locally