top | item 18580337

(no title)

adrice727 | 7 years ago

It's a far cry from actual pattern matching, but you can always tag your types[1]:

  interface Tagged {
    tag: string;
  }

  interface Circle extends Tagged {
    tag: 'circle'
  }

  // ... Other implementations 

  switch (myShape.tag) {
    case 'circle': {
      // Do circular things
    }
    // etc . . .
  }
You'll occasionally even see this in Scala code, since the @switch annotation optimizes pattern-match statements[2].

1. https://www.typescriptlang.org/docs/handbook/advanced-types....

2. https://www.scala-lang.org/api/2.12.x/scala/annotation/switc...

discuss

order

No comments yet.