Function either

  • The either schema is the equivalent of the TypeScript union (|). It allows you to validate that a value is of any type between a set of options.

    Example

    import { either, value, boolean } from "typerun/schema";
    import { is } from "typerun";

    const schema = either(value("hello"), value(42), boolean);
    if (is(schema)("hello")) {
    console.log("value matches one of the types");
    }
    if (is(schema)(42)) {
    console.log("value matches one of the types");
    }
    if (is(schema)(true)) {
    console.log("value matches one of the types");
    }
    if (!is(schema)(undefined)) {
    console.log("value does not match any of the types");
    }

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

    Type Parameters

    Parameters

    • Rest ...schemas: R

      All the schemas that the value can match.

    Returns Schema<Infer<R[number]>>