Home

Awesome

Enum proposal

Champion group & Authors

Motivation

  1. Provide enum (Enumerated type) in JavaScript.

    • This pattern is widely used in JavaScript.
  2. Provide Abstract Data Type based on enum.

    • It is implemented in many languages in different names like "enum variants" (Rust), "Data Constructor" (Haskell) or "tagged union" (TypeScript).

Compare to the previous proposals

Roadmap, design goal and relationship to the prior proposals #2

Please go to discussion forum for design details!

https://github.com/rwaldron/proposal-enum-definitions https://github.com/rbuckton/proposal-enum

The current proposal doesn't have a detailed design to be compared with. Currently I'm focusing on figuring out the design constraints before any semantics or syntax.

Design

Before the concreate design, we should decide the design constraints first.

Normal enum

ADT enum

  1. work well with TypeScript.
  2. work with structs proposal (constraint by @rbuckton and @syg), normal objects and Record & Tuples (for consistency).
  3. work well with pattern matching proposal.

Decided

Decided not to

To be decided

enum A { Case1 }
enum B extends A { Case2 }

B.Case1
enum A { A = 1, B = "a" }
enum A { B = 1 }
A.B === 1
some API to get the name of 1 === "B"
enum A { ["a" + "b"]: 1 }
enum A { B = expr() }
enum A { A = 0, B }; A.B === 1
enum A {} // valid TypeScript and flow.js today
enum A of symbol {} // valid flow.js today
enum A { B }; typeof A.B === ?
enum A { A, B, C }; for (const [key, value] of A);