Skip to content

Function: combinator() ​

Call Signature ​

ts
export declare function combinator<T, const R extends boolean>(config: CombinatorOptions<T> & {
  required: R;
}): WithRequiredOption<CombinatorSchema<T>, R>

Create a custom argument schema with a user-defined parse function.

This is the most general custom combinator. Use it when none of the built-in base combinators (string, number, integer, float, boolean, choice) fit your needs.

The returned schema has type: 'custom'.

Type Parameters ​

NameDescription
TThe parsed value type.
R extends booleanThe type of required in the configuration, which the schema keeps.

Parameters ​

NameTypeDescription
configCombinatorOptions<T> & { required: R }Configuration with a parse function and optional metavar.

Returns ​

WithRequiredOption<CombinatorSchema<T>, R> — A combinator schema that resolves to the parse function's return type.

Examples ​

ts
const date = combinator({
  parse: (value) => {
    const d = new Date(value)
    if (isNaN(d.getTime())) {
      throw new Error('Invalid date format')
    }
    return d
  },
  metavar: 'date'
})

Call Signature ​

ts
export declare function combinator<T, const R extends boolean | undefined = boolean | undefined>(config: CombinatorOptions<T> & {
  required?: R;
}): WithUnrequiredOption<CombinatorSchema<T>, R>

Create a custom argument schema with a user-defined parse function.

This is the most general custom combinator. Use it when none of the built-in base combinators (string, number, integer, float, boolean, choice) fit your needs.

The returned schema has type: 'custom'.

Type Parameters ​

NameDescription
TThe parsed value type.
R extends boolean | undefined = boolean | undefinedThe type of required in the configuration, 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
configCombinatorOptions<T> & { required?: R }Configuration with a parse function and optional metavar.

Returns ​

WithUnrequiredOption<CombinatorSchema<T>, R> — A combinator schema that resolves to the parse function's return type.

Examples ​

ts
const date = combinator({
  parse: (value) => {
    const d = new Date(value)
    if (isNaN(d.getTime())) {
      throw new Error('Invalid date format')
    }
    return d
  },
  metavar: 'date'
})

Call Signature ​

ts
export declare function combinator<T>(config: CombinatorOptions<T>): CombinatorSchema<T>

Create a custom argument schema with a user-defined parse function.

This is the most general custom combinator. Use it when none of the built-in base combinators (string, number, integer, float, boolean, choice) fit your needs.

The returned schema has type: 'custom'.

Type Parameters ​

NameDescription
TThe parsed value type.

Parameters ​

NameTypeDescription
configCombinatorOptions<T>Configuration with a parse function and optional metavar.

Returns ​

CombinatorSchema<T> — A combinator schema that resolves to the parse function's return type.

Examples ​

ts
const date = combinator({
  parse: (value) => {
    const d = new Date(value)
    if (isNaN(d.getTime())) {
      throw new Error('Invalid date format')
    }
    return d
  },
  metavar: 'date'
})

Released under the MIT License.