Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How does the type system work with union enums?


Asked by Brooks Brewer on Dec 03, 2021 FAQ



The other change is that enum types themselves effectively become a union of each enum member. With union enums, the type system is able to leverage the fact that it knows the exact set of values that exist in the enum itself. Because of that, TypeScript can catch bugs where we might be comparing values incorrectly.
Also,
This is something you can leverage, but it’s also very different from how number enums and the entire rest of TypeScript’s type system work. A simple union type gives you something that works similarly and is much more aligned with TypeScript.
Also Know, Union enums and enum member types 1 any string literal (e.g. "foo", "bar, "baz") 2 any numeric literal (e.g. 1, 100) 3 a unary minus applied to any numeric literal (e.g. -1, -100)
Thereof,
The body of an enum type declaration defines zero or more enum members, which are the named constants of the enum type. No two enum members can have the same name.
In addition,
This means after a compile step, you end up with the same code as before without the extra type definitions. Enums, like classes, create both a type and a value. Meaning that e.g. this declaration: emits code in the JavaScript output.