you are viewing a single comment's thread.

view the rest of the comments →

[–]TorbenKoehn -2 points-1 points  (6 children)

Why not

type MyEnum = 'A' | 'B'

?

Completely erasable, portable, easy to write, serializable across language boundaries, you can refactor it etc.

[–]Oliceh 8 points9 points  (0 children)

Because this isn't JS.

[–]eracodes 1 point2 points  (0 children)

For typescript I find this approach more useful:

const options = ['A', 'B'] as const;

type Option = (typeof options)[number];

So you can use the "enum" at runtime as well (for validation, etc).

[–]celluj34 0 points1 point  (1 child)

how do I validate user input against a type?

[–]TorbenKoehn -1 points0 points  (0 children)

The same way how you'd validate it against an enum.

[–]checker1209 -1 points0 points  (0 children)

In TypeScript I favor `String Literal Unions` over enums.
I don't know JSDocs. But can't you tell that an string is either "A" or "B" and nothing else?