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.
Creates a validating cast function from a guarding.
Applies
guardingto 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.Circular references are not supported. Validating objects with cycles causes stack overflow.
Validated objects are frozen. Subsequent mutations will silently fail in non-strict mode or throw in strict mode.
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
constdeclarations.