index.d.ts (806B)
1 /** 2 Define a [lazily evaluated](https://en.wikipedia.org/wiki/Lazy_evaluation) property on an object. 3 4 @param object - Object to add the property to. 5 @param propertyName - Name of the property to add. 6 @param valueGetter - Called the first time `propertyName` is accessed. 7 8 @example 9 ``` 10 import defineLazyProperty from 'define-lazy-prop'; 11 12 const unicorn = { 13 // … 14 }; 15 16 defineLazyProperty(unicorn, 'rainbow', () => expensiveComputation()); 17 18 app.on('user-action', () => { 19 doSomething(unicorn.rainbow); 20 }); 21 ``` 22 */ 23 export default function defineLazyProperty< 24 ObjectType extends Record<string, any>, 25 PropertyNameType extends string, 26 PropertyValueType 27 >( 28 object: ObjectType, 29 propertyName: PropertyNameType, 30 valueGetter: () => PropertyValueType 31 ): ObjectType & {[K in PropertyNameType]: PropertyValueType};