6 ms·
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
by adrice727 8y 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.html#discriminated-unions https://www.typescriptlang.org/docs/handbook/advanced-types....
2. https://www.scala-lang.org/api/2.12.x/scala/annotation/switch.html https://www.scala-lang.org/api/2.12.x/scala/annotation/switc...