@metreeca/type - v0.1.1
    Preparing search index...

    Function as

    • Creates a validating cast function from a guarding.

      Applies guarding to ensure input matches the expected type, throwing on invalid input. For plain objects, memoizes validation results by branding with the guarding, so subsequent calls return immediately without re-validation. Validated objects are also frozen. Non-object values are validated on every call.

      Caution

      Circular references are not supported. Validating objects with cycles causes stack overflow.

      Warning

      Validated objects are frozen. Subsequent mutations will silently fail in non-strict mode or throw in strict mode.

      Important

      For memoization to work, guards must have stable identity. Local functions and lambdas are handled without memory leaks, but won't be memoized since they lack persistent identity. Use module-level named guards or const declarations.

      Type Parameters

      • T

        The type being validated

      Parameters

      • guarding: Guarding<T>

        The guarding to use for validation

      Returns (v: unknown) => T

      A cast function that throws on invalid input

      When the guarding returns an error report

      When the validated value contains circular references