• Creates a read-only non-extensible record with properties based on the given definitions. If the property in the definition is a function, it will only be executed once lazily, i.e. on the first evaluation of the respective property.

    Trying to change or add a property will result in both compiler and runtime errors.

    Example

    let id = 0;
    const getId = () => ++id;
    // mix lazy functions with plain values
    const obj = lazyRecord({ id: getId, name: 'TypeScript' });
    expect(id).toBe(0); // getId not called yet, so global id is still 0
    expect(obj.id).toBe(1); // calls getId once
    expect(obj.id).toBe(1); // won't be called again and value stays the same
    expect(id).toBe(1); // getId only called once, so global id is still 1

    Type Parameters

    Type Parameters

    • T extends Record<string | number | symbol, any>

    Parameters

    • definition: T

      an object with fixed values, or functions to produce the values

    Returns Readonly<{ [ K in keyof T]: T[K] extends (() => infer R) ? R : T[K] }>

Generated using TypeDoc