Function tuple

  • The tuple schema checks that the type of the value is an array, and that all of its elements match the given schemas. The schemas are defined in a tuple. The order of the schemas in the tuple must match the order of the elements in the array. The schemas can be of any type.

    Example

    import { tuple, string, number } from "typerun/schema";
    import { is } from "typerun";

    const schema = tuple(string, number);
    if (is(schema)(["hello", 42])) {
    console.log("value is a tuple of a string and a number");
    }
    if (!is(schema)(["hello", "world"])) {
    console.log("value is not a tuple of a string and a number");
    }

    If you use the validate function, the errors contain all of the different type validation fails from the tuple.

    Tuples can be combined with any other schemas, to create complex validation patterns.

    import { tuple, string, number, array } from "typerun/schema";
    import { is } from "typerun";

    const schema = tuple(string, array(number));
    if (is(schema)(["hello", [42, 31]])) {
    console.log("value is a tuple of a string and an array of numbers");
    }

    Type Parameters

    Parameters

    • Rest ...schemas: S

      The schemas to validate the elements of the tuple.

    Returns Schema<ReturnValue<S>>