title: how to memoize a TypeScript getter
body: <p>I am using the following approach to memoize a TypeScript getter using a decorator but wanted to know if there is a better way. I am using the popular <a href="https://www.npmjs.com/package/memoizee" rel="nofollow noreferrer">memoizee</a> package from npm as follows:</p>
  
  <pre><code>import { memoize } from '@app/decorators/memoize'
  
  export class MyComponent {
  
    @memoize()
    private static memoizeEyeSrc(clickCount, maxEyeClickCount, botEyesDir) {
      return clickCount &lt; maxEyeClickCount ? botEyesDir + '/bot-eye-tiny.png' : botEyesDir + '/bot-eye-black-tiny.png'
    }
  
    get leftEyeSrc() {
      return MyComponent.memoizeEyeSrc(this.eyes.left.clickCount, this.maxEyeClickCount, this.botEyesDir)
    }
  }
  </code></pre>
  
  <p>AND the memoize decorator is:</p>
  
  <pre><code>// decorated method must be pure
  import * as memoizee from 'memoizee'
  
  export const memoize = (): MethodDecorator =&gt; {
    return (target: any, propertyKey: string, descriptor: PropertyDescriptor) =&gt; {
      const func = descriptor.value
      descriptor.value = memoizee(func)
      return descriptor
    }
  }
  </code></pre>
  
  <p>Is there a way to do this without using two separate functions in MyComponent and to add the decorator directly to the TypeScript getter instead?</p>
  
  <p>One consideration here is that the decorated function must be pure (in this scenario) but feel free to ignore that if you have an answer that doesn't satisfy this as I have a general interest in how to approach this problem.</p>