Skip to content

Function: choice()

WARNING

This API is experimental and may change in future versions.

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

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

Signature

ts
declare function choice<const T extends readonly string[]>(values: T, opts?: BaseOptions): CombinatorSchema<T[number]>

Type Parameters

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

Parameters

NameTypeDescription
valuesTAllowed values.
optsBaseOptionsCommon options (description, short, required). (optional)

Returns

CombinatorSchema<T[number]> — A combinator schema that resolves to a union of the allowed values.

Examples

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

Released under the MIT License.