3 ms·
TS is great but lack of pattern matching cripples the language somewhat
by AzzieElbab 5y ago
TS is great but lack of pattern matching cripples the language somewhat
- a_humean 5y agoThe tc39 pattern matching proposal seems to have some renewed energy with new champions (including from the typescript team) and syntax proposals: https://github.com/tc39/proposal-pattern-matching https://github.com/tc39/proposal-pattern-matching Also there has been movement in the user land space with libraries like ts-pattern that make use of new features in typescript 4.x to provide basically the full pattern matching experience: https://github.com/gvergnaud/ts-pattern https://github.com/gvergnaud/ts-pattern
- dtech 5y agoIt helps that is has good smart-casting, you can do with an if where other languages need pattern matching const x: { t: 'A', foo: string } | { t: 'B', bar: string } = /*...*/ return x.t === 'A' ? x.foo : x.bar
- AzzieElbab 5y agoIt is cool syntax trick, but it won’t make compiler tell you when you missed a case