Skip to content
TC-39 Proposal for Discriminated Unions
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples
.gitignore
LICENSE
README.md

README.md

Proposal for Discriminated Unions

Serious Work in Progress

Status

Stage: 0 Champion: unassigned

Authors

  • Chris Andrejewski (@andrejewski)

Previous Discussions

Motivations

Adoption

Proposal

Example

enum Msg (
  Increment,
  Decrement
)

function update (msg, count) {
  const newCount = match msg {
    Increment (n = 1) => count + n
    Decrement (n = 1) => count - n
  }
  return Math.max(0, Math.min(newCount, 100))
}

update(Msg.Increment, 1) // 2
update(Msg.Increment(5), 1) // 6
update(Msg.Decrement, 1) // 0
update(Msg.Decrement(5), 1) // -4
You can’t perform that action at this time.