Enumerated Types
When modeling state machines it can be useful to describe variants of a single type that can contain different values. This concept is simple enough to describe optional types or complex enough to model multi-state systems.
Definition
Use the enum
keyword to define enumerated types with one or more variants.
- Knot
- TypeScript
knot
enum CardSuit =| Spade| Diamond| Club| Heart;
knot
enum CardSuit =| Spade| Diamond| Club| Heart;
tsx
enum CardSuit {Spade,Diamond,Club,Heart,}
tsx
enum CardSuit {Spade,Diamond,Club,Heart,}
Constructor Arguments
Each enum variant can optionally specify some data that it must take as arguments during construction, and can be extracted when destructuring.
- Knot
- TypeScript
knot
enum Primitive =| Nil| Boolean(boolean)| Integer(integer)| Float(float)| String(string);
knot
enum Primitive =| Nil| Boolean(boolean)| Integer(integer)| Float(float)| String(string);
tsx
/* there is no built-int TypeScript equivalent to this */
tsx
/* there is no built-int TypeScript equivalent to this */