Skip to content

gunshi / combinators / choice

Function: choice()

ts
function choice<T>(values, opts?): CombinatorSchema<T[number]>;

Experimental

Create an enum-like argument schema with literal type inference.

Uses const T generic to infer literal union types from the values array.

Type Parameters

Type ParameterDescription
T extends readonly string[]The readonly array of allowed string values.

Parameters

ParameterTypeDescription
valuesTAllowed values.
opts?BaseOptionsCommon options (description, short, required).

Returns

CombinatorSchema<T[number]>

A combinator schema that resolves to a union of the allowed values.

Example

ts
const args = {
  level: choice(['debug', 'info', 'warn', 'error'] as const)
}
// typeof values.level === 'debug' | 'info' | 'warn' | 'error'

Released under the MIT License.