Meaningful Object Keys in TypeScript
In Typescript, Record
is a common way to express an object type. For example:
The disadvantage of using a Record
in some cases is that you lose the benefit of expressing the meaning of the key. Let’s take the following example:
Looking at this code, I can’t know that the string
type inside the memory Record
refers to a Lambda’s id
.
There are two possible solutions. The first is to use an index signature:
This approach has the advantage that you can choose the property’s name. Consequently, you are limited to using a string, number, or symbol as the type.
The second is to point to the Lambda id
type directly:
Now I can understand the relationship in my schema more easily.
Follow me on Medium or Twitter to read more about Angular and JS!