Loading video...

Video Failed to Load

Go Home

❌ Avoid difficult-to-read conditionals in React. ✅ Instead you can use ts-pattern to express complex conditions in a single, compact expression.

185,781 views • 2 years ago •via X (Twitter)

11 Comments

George Moller's profile picture
George Moller2 years ago

Hey there 👋 Friendly reminder that I have more than 100 infographics and video animations just like this one. Check them out ↓

thgmhz's profile picture
thgmhz2 years ago

in some scenarios, you can avoid conditionals setting an object and getting the value by association:

Michael Lynch's profile picture
Michael Lynch2 years ago

Personally, I wouldn't introduce a library for something that simple. Nothing wrong with a basic switch or if...else statement. Just don't use a nested ternary.

George Moller's profile picture
George Moller2 years ago

For most cases I use maps, for more complex pattern matching I use ts-pattern.

Marco Pfeiffer's profile picture
Marco Pfeiffer2 years ago

Just use If conditions. No library that everyone reading has to learn, no overhead, more readable than both examples. Maybe a bit longer, but if you have a lot of those conditions, then the approach is wrong anyways. A separate Map would be in order.

James Vanderzon's profile picture
James Vanderzon2 years ago

just avoid React 🤪

George Moller's profile picture
George Moller2 years ago

😅 what library or framework do you use in the frontend? (If any)

Geoffrey Shepard's profile picture
Geoffrey Shepard2 years ago

Nice explanation. I will say the hardest thing to read might be the "ss" font there in the word "address"

George Moller's profile picture
George Moller2 years ago

haha font ligatures right? I have mix feeling about them, sometimes I like them, sometimes I don't...

Shripal Soni's profile picture
Shripal Soni2 years ago

I didn't know about `ts-pattern` before. Thanks for sharing about it, George 🙌

George Moller's profile picture
George Moller2 years ago

It's a great solution for complex pattern matching for sure! try it out!

Related Videos