TS-Pattern is over 3K ⭐️ on GitHub now! This is nuts 🤯
I started TS-Pattern as an experiment to see how far the type system of #TypeScript could go.
Here are 4 of my favorites features I've built into TS-Pattern! 🧵👇
First, keep in mind that Pattern Matching is usually a language feature.
Trying to make a library as good as native support is challenging! A key aspect of this is type inference.
The features I'll talk about have little to no runtime. It's only types!
Feature nº1: Type narrowing 🔎
TS-Pattern infers a precise type for your input based on the pattern you provide:
Feature nº2: Exhaustiveness checking ✅
If your list of patterns doesn’t cover all edge cases of your input data-structure, TS-Pattern is able to tell you what cases are missing to make your branching exhaustive:
Feature nº3: Select ✂️
The `P.select` pattern let you extract a part of your input and TS-Pattern will inject it in your handler with the right type!
Feature nº4: Matching on unknown inputs 🤔
TS-Pattern can help you validate the shape of unknown inputs. You can infer a type from a pattern and use isMatching to narrow your input down in an `if` statement:
✅ Unreachable branches do not type-check anymore
✅ `.with ` and `.otherwise` inherit type narrowing from previous branches
✅ ~15% type-checking performance improvement