Gabriel Vergnaud Profile picture
TypeScript nerd • Author of TS-Pattern • Teacher • Staff Eng @datadoghq Learn how to unleash TypeScript's full potential 👉 https://t.co/0GmYbCJKEj

Aug 29, 2022, 8 tweets

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:

That's it for now!

Check out the repo 👉 github.com/gvergnaud/ts-p…

If you want to learn how I do this, stay tuned! I'm working on a series ob blogposts on advanced #TypeScript patterns... 🤫

Well, that was fast 🔥

TS-Pattern is over 4k stars now ✨🎉

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling