Skip to content

Function: choice() ​

Call Signature ​

ts
export declare function choice<const T extends readonly string[], const R extends boolean>(values: T, opts: BaseOptions & {
  required: R;
}): WithRequiredOption<CombinatorSchema<T[number]>, R>

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 ​

NameDescription
T extends readonly string[]The readonly array of allowed string values.
R extends booleanThe type of required in the options, which the schema keeps.

Parameters ​

NameTypeDescription
valuesTAllowed values.
optsBaseOptions & { required: R }Common options (description, short, hidden, required).

Returns ​

WithRequiredOption<CombinatorSchema<T[number]>, R> — 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'

Call Signature ​

ts
export declare function choice<const T extends readonly string[], const R extends boolean | undefined = boolean | undefined>(values: T, opts?: BaseOptions & {
  required?: R;
}): WithUnrequiredOption<CombinatorSchema<T[number]>, R>

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 ​

NameDescription
T extends readonly string[]The readonly array of allowed string values.
R extends boolean | undefined = boolean | undefinedThe type of required in the options, which the schema keeps when it may be false but not true, as for optional ? { required: false } : {}, so that a positional argument with it is optional.

Parameters ​

NameTypeDescription
valuesTAllowed values.
optsBaseOptions & { required?: R }Common options (description, short, hidden, required). (optional)

Returns ​

WithUnrequiredOption<CombinatorSchema<T[number]>, R> — 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'

Call Signature ​

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

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 ​

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

Parameters ​

NameTypeDescription
valuesTAllowed values.
optsBaseOptionsCommon options (description, short, hidden, 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.