Dictionaries are commonly used collections. They provide the benefit of quickly looking up values based on a supplied Key and these lookups are extremely fast as they don't rely on iterating the collection to locate them. In Typescript this is harder to achieve. I have seen several implementations but many of them use separate backing arrays to hold the keys and the values and either iterate or use an indexOf call to locate the indexes. This can be avoided if you're willing to make the sacrifice of limiting yourself to the string data type for your key. Let's explore how this can be accomplished. More...
Recently, we've been migrating a large (and still in progress) Angular 1.x application from JavaScript to TypeScript. This has allowed us to seriously clean up our code, enforce some more consistent patterns across teams, and provide compile time validation within our code. As we migrated, we realized that some of our service classes had a large number of dependencies injected into them, from other logic services to validation services to data services. Each of these services also may have many dependencies. While Angular does create these as singletons and cache them, many of them may only be used for one or two logic methods so it made sense to use a factory and instantiate them lazily. To accomplish this, I created a couple of helpers. Here's how I did it. More...