Earlier quoted context omitted.
I played with it a bit because I really like TS's union, literal types and smart-casting, unfortunately it doesn't seem quite there yet and I found it only usable for T | null types. Syntax is all there so maybe it'll get better in 3.1+
Would be interesting if you can provide some examples. I'm happy to check them and turn them into tickets / feature requests for you.
val result: Int | "INTERNAL_SERVER_ERROR" = 1
val m: Int = result match {
case "INTERNAL_SERVER_ERROR" => throw new Exception("Uh oh")
case t => t
}
It does work with a ClassTag and t: T, but then it thinks the match is not exhaustive. Compare that to Typescript [2].In this specific language feature TS is kinda the gold standard. For example they can automatically narrow the type based on fields existing or not and their type, which can be used to provide a pretty decent implementation of sum types [3]
[1] https://scastie.scala-lang.org/oMdkLBIiRsqUkXv6qbP61g
[2] https://www.typescriptlang.org/play?#code/MYewdgzgLgBATgUwgV...
[3] https://www.typescriptlang.org/docs/handbook/unions-and-inte...