Skip to content

lazy()

ts
function lazy<T>(constructFn): T

Creates a lazy-loaded function that initializes the function only when it is called for the first time. With lazy, you can create recursive parsers without running into circular dependencies. Also useful to lazily initialize just-in-time compiled parsers and guards.

Example

Create recursive parsers with lazy. Note that you must use explicit type annotations:

ts
import { lazy, type Parser, object, parseString, optional } from 'pure-parse'

type Person = {
  name: string
  father?: Person
  mother?: Person
}
const parsePerson: Parser<Person> = lazy(() =>
  object({
    name: parseString,
    father: optional(parsePerson),
    mother: optional(parsePerson),
  }),
)

Type Parameters

Type Parameter
T extends (...args) => unknown

Parameters

ParameterTypeDescription
constructFn() => T

Returns

T