Dealing with types can be a bit tricky.
One common option is to use A_A.
If using a type
is not necessary, you can utilize an enum
instead, allowing for easy iteration over its values.
For example:
enum Animals {
Cat = 'cat',
Dog = 'dog',
}
const animals: Animals[] = (Object.keys(Animals) as Array<keyof typeof Animals>).map(x => Animals[x])
// animals = ['cat', 'dog']
To access values in an enum
, you can reference them by their string value or the corresponding property name.
For instance, both of these methods are valid:
const animals: Animals[]
// First option:
if (animals[0] === 'cat') {
...
}
// Second option:
if (animals[0] === Animals.Cat) {
...
}
Notes:
- Unlike
const enum
(which cannot be iterated over), using regular enum
types will add JavaScript code to the bundle, something TypeScript typically avoids. Consider the implications if your project contains many enums as it may increase bundle size.