6

What would be the most straightforward way of implementing Swift-style enums with associated values in Typescript? Is it possible?

Within enums with associated values, I'm looking for the specific case of indirect enum with associated values that can work with ASTs — see example below:

indirect enum Node {
    case op(Node, Node)
    case value(Double)
}

I found an answer to a related question but it's about Javascript support rather than Typescript support. I don't think this is a duplicate of that question; there may be some solutions that work in TypeScript but not in JavaScript.

Franklin Yu
  • 8,920
  • 6
  • 43
  • 57
marco alves
  • 1,707
  • 2
  • 18
  • 28
  • I would like to add an answer to this question, which does not work in pure JavaScript. – Franklin Yu Aug 17 '18 at 22:54
  • Never mind; posted it on [How do you emulate ADTs and pattern matching in TypeScript](https://stackoverflow.com/questions/21034017/how-do-you-emulate-adts-and-pattern-matching-in-typescript/). – Franklin Yu Aug 17 '18 at 23:06

0 Answers0