DEV Community

Cover image for I've been writing TypeScript without understanding it -- pt. 2

I've been writing TypeScript without understanding it -- pt. 2

vincanger on July 09, 2024

Give me a Break. I'm still learning! Hey everyone. I'm back. And, yeah, I'm still making n00b TypeScript mistakes 😢 But luckily I've...
Collapse
 
johnlandgrave profile image
John Landgrave

This works for basic exhaustiveness checks, but when things are more complicated (or depend on two things, e.g. a boolean called isSubscribed in combination with an optional plan tier) we've found that ts-pattern is a great library for handling that kind of stuff in a way where the compiler still gives you an exhaustiveness check on your enum, for example.

Also, it allows you to disambiguate arguments, variables, API responses, etc. at run-time structurally (similarly to the way TS does it at design time) which is pretty neat. And you don't have to go full nerd and really understand what things like "contravariance" mean!

Collapse
 
vincanger profile image
vincanger

oh wow. nice! I'll have to check this out.

Collapse
 
karadza profile image
Juraj

Love this series!
Keep em coming 😄

Collapse
 
vincanger profile image
vincanger

Glad you like it :)

Collapse
 
matijasos profile image
Matija Sosic

I love this "Watch me learn TypeScript" series, and a relaxed tone you use with it. Plus I also learn stuff :)

Collapse
 
vincanger profile image
vincanger

It's never too late to teach an old dog new tricks 🐶

Collapse
 
chris_sd_b9194652dbd4a1e profile image
Chris S-D

Very cool idea. Actually leveraging your lack of understanding of something to sell it and also learn at the same time.

One convention I'd like to mention is that for your PaymentPlanId enum, it is commonly assumed that ids represent a non limited identifier which is typically not a good idea for an enum. A more common approach to indicate that an identifier is limited to a short list that will likely not change much is Code. So, it might be better to call it PaymentPlanCode.

Again, this is just a common convention. There is no rule that says you must do it this way, however, you'll often find that conventions like these help convey meaning without bloating code.

Collapse
 
vincanger profile image
vincanger

ah nice, that makes sense. thanks for the tip!

Collapse
 
alyssaseholm profile image
AlyssaSeholm

This series is a delight to read and is also teaching me a lot. I really appreciate the examples with use-cases 😊🤌

Collapse
 
vincanger profile image
vincanger

thanks. glad you like it!

Collapse
 
mrdeeds88 profile image
mrdeeds88

Thanks for mentioning Wasp. I'm doing some frameworks try out this time.

Collapse
 
geekymonkey profile image
Russ Painter

Great stuff!

Collapse
 
vincanger profile image
vincanger

thanks!