DEV Community

Discussion on: It's 2021 and I still don't know the best way to write this in TypeScript.

Collapse
 
reggi profile image
Thomas Reggi

Thanks for the reply! Yes memoization / caching of getters is cool but I often think of it as too much boiler plate, then there's the async issue 🙄. But I do appreciate your other example of using field initializers outside the constructor.

Collapse
 
lionelrowe profile image
lionel-rowe

Yeah memoization is only really suitable where you need the performance boost. If you do need it though, you only need to write the boilerplate once, or zero times if you use a library. It can be adapted for asynchronous usage, as long as the memoized function always produces the same output from the same inputs. That wouldn't be the case when calling a web API, but might be the case if the asynchrony is due to carrying out expensive computation on a worker thread or something.