lazyRecord<T>(definition: T): Readonly<{[ K in keyof T]: T[K] extends (() => infer R) ? R : T[K]}>
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
letid = 0; constgetId = () => ++id; // mix lazy functions with plain values constobj = 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]}>
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