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…
Third, you need to know how to parse type-level strings!

Chapters 5, 6, and 7 show you how to use the type system as a full fledged programming language to solve this kind of problems
You'll learn about Conditional Types and how to use the `infer` keyword to pattern-match on types 👉 type-level-typescript.com/conditional-ty…

Write arbitrary loops with Recursive Types 👉 type-level-typescript.com/recursive-types

And use all of this to parse strings! 👉 type-level-typescript.com/template-liter…
Supporting unions of objects is tricky, even lodash's `get` function doesn't handle this case well.

To solve this you'll need a super accurate mental model of the way union types work.

This is exactly what the 8th chapter of Type-Level TypeScript will be about!
Chapter 8 will be out soon, but in the meantime here is a TypeScript Playground with all the code used in the video

👉 typescriptlang.org/play?#code/JYW… Image
Once you start looking at the type system as a programming language, you realize that this isn't black magic, it's just regular code! Anyone can learn this 🧙‍♂️

• • •

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
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
Aug 29, 2022
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:
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 on Twitter!

:(