index.d.ts 802 B

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