News for hackers. Stuff that matters.
Re:Pattern Matching Custom Data Types in TypeScript by sfvisser on Saturday February 02, 2019 @04:25PM (#19065813)
It's a neat trick and I've used similar patterns in some of our projects, but in the end it doesn't buy you much over a `switch (x.kind) { ... }`. (Except your matcher can be used as a proper expression which is nice obviously)

But you don't have proper pattern nesting, fall-through semantics and it's not clear (to me yet) how to extend to matching on multiple values. Try implementing an equality function on a sum-type using pattern matching.

Having actual proper pattern matching added to TS would be such a great addition. Especially with full support of the control flow analysis.

Re:Pattern Matching Custom Data Types in TypeScript by alexsasharegan on Saturday February 02, 2019 @04:31PM (#19065851)
This might be our best hope right now: https://github.com/tc39/proposal-pattern-matching
Re:Pattern Matching Custom Data Types in TypeScript by Klathmon on Saturday February 02, 2019 @07:13PM (#19066817)
I see a lot of people asking for this in TS, but i'd much rather it be in plain JS and let TS add anything on top of it that is needed.

Pattern matching would work SO WELL in javascript, especially considering how frequently switch is currently poorly used as a replacement in things like Redux.

Re:Pattern Matching Custom Data Types in TypeScript by moogly on Sunday February 03, 2019 @08:45AM (#19069509)
Yeah, but there's no way that can be the final syntax, because it's absolutely horrible (TS could always have a Rust/C# 8 one on top though).