Hacker News new | past | comments | ask | show | jobs | submit login

TS is great but lack of pattern matching cripples the language somewhat



The 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

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


It 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


It is cool syntax trick, but it won’t make compiler tell you when you missed a case




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: