Loading docs…
Loading docs…
Defer creation of a predicate until its first evaluation. This lets a recursive guard refer to itself while it is being defined.
import { arrayOf, isString, lazy, typedStruct } from 'is-kit';import type { Predicate } from 'is-kit';type Tree = {readonly value: string;readonly children: readonly Tree[];};const isTree: Predicate<Tree> = lazy(() =>typedStruct<Tree>()({value: isString,children: arrayOf(isTree),}),);isTree({value: 'root',children: [{ value: 'leaf', children: [] }],}); // true
The factory runs on first use, and the returned predicate is cached after successful initialization. If the factory throws, the error is propagated and a later evaluation can retry initialization.
lazy makes recursive guard definitions possible. It does not detect cycles in the input value; validating cyclic data requires separate cycle handling.