Last change
on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
802 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | /**
|
---|
| 2 | Define a [lazily evaluated](https://en.wikipedia.org/wiki/Lazy_evaluation) property on an object.
|
---|
| 3 |
|
---|
| 4 | @param object - Object to add property to.
|
---|
| 5 | @param propertyName - Name of the property to add.
|
---|
| 6 | @param fn - Called the first time `propertyName` is accessed.
|
---|
| 7 |
|
---|
| 8 | @example
|
---|
| 9 | ```
|
---|
| 10 | import defineLazyProp = require('define-lazy-prop');
|
---|
| 11 |
|
---|
| 12 | const unicorn = {
|
---|
| 13 | // …
|
---|
| 14 | };
|
---|
| 15 |
|
---|
| 16 | defineLazyProp(unicorn, 'rainbow', () => expensiveComputation());
|
---|
| 17 |
|
---|
| 18 | app.on('user-action', () => {
|
---|
| 19 | doSomething(unicorn.rainbow);
|
---|
| 20 | });
|
---|
| 21 | ```
|
---|
| 22 | */
|
---|
| 23 | declare function defineLazyProp<
|
---|
| 24 | ObjectType extends {[key: string]: unknown},
|
---|
| 25 | PropertyNameType extends string,
|
---|
| 26 | PropertyValueType
|
---|
| 27 | >(
|
---|
| 28 | object: ObjectType,
|
---|
| 29 | propertyName: PropertyNameType,
|
---|
| 30 | fn: () => PropertyValueType
|
---|
| 31 | ): ObjectType & {[K in PropertyNameType]: PropertyValueType};
|
---|
| 32 |
|
---|
| 33 | export = defineLazyProp;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.