Gabriel Vergnaud Profile picture
Aug 29, 2022 8 tweets 3 min read Read on X
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! 🧵👇 Image
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 ✨🎉 Image

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Gabriel Vergnaud

Gabriel Vergnaud Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @GabrielVergnaud

Jan 9, 2023
TS-Pattern's inference just got a bit smarter!

✅ Unreachable branches do not type-check anymore
✅ `.with ` and `.otherwise` inherit type narrowing from previous branches
✅ ~15% type-checking performance improvement

Release note 👉 github.com/gvergnaud/ts-p…
This is not a breaking change!

If your project doesn't type-check after upgrading, it's probably that you had duplicated/unreachable code branches.

If you think it's a bug feel free to open an issue here 👉 github.com/gvergnaud/ts-p…
This feature was part of my wish list for TS-Pattern v5.

It turns out adding it to the current version didn't require too much effort, and it actually leads to *better* type-checking performance! 🎉

Read 4 tweets
Jan 2, 2023
Implementing a get function that narrows union types and rejects invalid paths is tricky.

But it's doable if you know some type-level programming!

Want to become a #TypeScript expert in 2023?

Here is what you need to know to type this function
👇
First, you need a precise understanding of how assignability works in TypeScript.

Types like `never` or `unknown` are super useful, but often misunderstood!

The second chapter of Type-Level TypeScript covers this in depth (you can read it for free!)

type-level-typescript.com/types-are-just…
Second, you need to know how to manipulate type-level data structures like objects and tuples.

You can learn this by reading chapter 3 and 4 (still free)!

type-level-typescript.com/objects-and-re…

type-level-typescript.com/arrays-and-tup…
Read 8 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(