index.d.ts 806 B

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