Function optional

  • The optional schema checks that the type of the value is either the given type, or undefined. It is the equivalent of the TypeScript question mark (?).

    Example

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

    const maybeString = optional(string);
    if (is(maybeString)(undefined)) {
    console.log("maybe a string");
    }
    if (is(maybeString)("hello")) {
    console.log("maybe a string");
    }
    if (!is(maybeString)(null)) {
    console.log("not maybe a string");
    }

    Type Parameters

    • R

    Parameters

    • schema: Schema<R>

      The optional schema.

    Returns Schema<undefined | R>