Can you explain the distinction between using typeof
with a constant and an enum
in TypeScript?
For example:
const TYPE_A = 'a'
const TYPE_B = 'b'
type MyType =
| typeof TYPE_A
| typeof TYPE_B
type Result = {
name: string
type: MyType
}
// and
enum QuestionType {
A = "a",
B = "b",
}
type Result = {
name: string
type: MyType
}
I am interested in using the values "a"
and "b"
in multiple parts of the code. I was thinking about using typeof variable
, but I'm not sure if that is the best approach.
I have also come across const enum
, but I'm not clear on the differences between these options.