Comment by shepherdjerred

8 hours ago

I love how far Java has come. I still prefer TypeScript since I do a lot of frontend work and prefer to use the same language across the stack.

With the said, you can do exhaustive pattern matching in TypeScript! https://github.com/gvergnaud/ts-pattern

> I still prefer TypeScript since I do a lot of frontend work and prefer to use the same language across the stack.

If you perfer using the same language for both frontend and backend work, yet would like to have one more semantically rich than TypeScript, consider Scala.

For the frontend, ScalaJS[0].

For the backend, Scala[1].

0 - https://www.scala-js.org/

1 - https://scala-lang.org/

Isn't that overkill? You can check exhaustiveness with a trivial little helper:

    function exhaustive(witness: never): never {
      return witness
    }

https://www.typescriptlang.org/play/?#code/FAFwngDgpgBAKgZQB...

You don't need a library for this; the idiomatic approach is to lean on TypeScript's narrowing and native control flow (ifs and switches, or maybe IIFEs), along with (maybe) the never type to enforce exhaustiveness.

https://www.typescriptlang.org/play/?jsx=4#code/JYWwDg9gTgLg...

I guess the library is slightly nicer, arguably, but I don't think it's worth the external dependency and added complexity.

I knew I should've written a disclaimer about doing this in vanilla TypeScript

Yes, you can do it via an assert never in a switch. That isn't pattern matching. That's an exhaustive switch enforced at compile time

Which is nice, but again, it's not pattern matching.