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
| Name | Description |
|---|---|
T extends readonly string[] | The readonly array of allowed string values. |
R extends boolean | The type of required in the options, which the schema keeps. |
Parameters
| Name | Type | Description |
|---|---|---|
values | T | Allowed values. |
opts | BaseOptions & { 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
| Name | Description |
|---|---|
T extends readonly string[] | The readonly array of allowed string values. |
R extends boolean | undefined = boolean | undefined | The 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
| Name | Type | Description |
|---|---|---|
values | T | Allowed values. |
opts | BaseOptions & { 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
| Name | Description |
|---|---|
T extends readonly string[] | The readonly array of allowed string values. |
Parameters
| Name | Type | Description |
|---|---|---|
values | T | Allowed values. |
opts | BaseOptions | Common 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'