Use an Enum as Object Key in TypeScript
Objects are a fast and convenient data type in JavaScript. You often want to put a restriction on what keys can be used. You can use TypeScript enums in two ways to define the keys in an object.
Using the in
nomenclature:
enum Animal {Bird,Cat,Dog,}type AnimalCount = { [key in Animal]: number }const animals: AnimalCount = {[Animal.Bird]: 1,[Animal.Cat]: 3,[Animal.Dog]: 5,}
The other syntax creates a slightly different object:
enum Animal {Bird,Cat,Dog,}type AnimalCount = { [key in keyof typeof Animal]: number }const animals: AnimalCount = {Bird: 1,Cat: 3,Dog: 5,}
The keys are limited by the enum keys, but you can't use [Animal.Bird]
as a
key.