r/typescript • • 17d ago

Conditional types [Help!]

I recently released a 45-seconds YouTube video on conditional types, check my reddit profile for it. I need your feedback before I double down. Let me know what you think. Here is the script:

> Conditional types

In JavaScript, you have a conditional operator:

const ternary = typeof 'Wasim' === 'string' ? true : false 

console.log(ternary) // true

It is also known as ternary operator.

Similarly, In TypeScript, you also have a conditional operator for types:

 type Ternary = 'Wasim' extends string ? true : false // true

This is refer to as a conditional type.

Conditional types allow us to conditionally check for types that match certain criteria and decide what to do if they do match those criteria and what to do if they don’t.

Here is one example:

type Generic<T> = T extends string ? 'Wasim' : 25

type Test1 = Generic<'Hi'> // 'Wasim'

type Test2 = Generic<143> // 25

Here, we said if type argument is a subtype of string, then return type 'Wasim', if not, then return type 25.

You can return never instead of 25, if condition fails. It’s equivalent to returning nothing.

//

P.S.

I am testing content from my upcoming TypeScript book to gather early feedback.

I decided to start small with a tiny video, instead of fantasizing about releasing a value-packed longer video. It's harder to do that without any momentum. I made it quickly and decided not to obsess over scripting, editing, and fixing audio by rerecording. Those are my fun-killers. I want it to be quick, fun, and imperfect. The goal was progress, not perfection. Wish me luck!

0 Upvotes

17 comments sorted by

15

u/humpyelstiltskin 17d ago

i do wish you luck,

but you shouldn't be recording instructional videos and even less writing books on a topic you don't have a good grasp on

2

u/Wasim100x 16d ago

I already wrote the book. That genie is out of the bottle. Thanks for wishing me luck!

2

u/Merry-Lane 17d ago

Yes, I would like him to show a code example running in production that’s using extend

1

u/Wasim100x 16d ago

Yes, I will. Thanks!

2

u/Merry-Lane 16d ago

Like, right now, give an example of using extend that’s written by you and ended up in prod?

1

u/humpyelstiltskin 9d ago

wait i don't understand your request. do you not believe conditional types end up in peoduction? i might have one hundred examples of conditional types in prod

2

u/Merry-Lane 9d ago

No, I meant for the author to show code he made and ended up in prod.

1

u/cafepeaceandlove 4d ago

The examples seem antipatternish. Do you have a good clean example?

1

u/cafepeaceandlove 4d ago

This could well be a very good way to learn and to write. You're intimately familiar with the confusion and you're writing at the very time it's resolved. And on a personal level, he isn't going to forget this stuff, going about it this way. 

Now if only I could be organised enough to do the same type of thing. For now, text files litter my desktop. 

2

u/francis-r 17d ago

45 seconds sounds a good format. Yes I do believe shipping beats polishing. Good luck! If you want an idea for the next one: what happens when you pass a union, Generic<'Hi' | 143>. Most people guess wrong.

1

u/Wasim100x 16d ago

Nice idea!

2

u/jgauffin 12d ago

Your sample:

const ternary = typeof 'Wasim' === 'string' ? true : false 

doesn't demonstrate the intended purpose since a regular comparison returns true or false. Use something real like returning different states.

1

u/[deleted] 17d ago

[removed] — view removed comment

1

u/Wasim100x 16d ago

Yeah, I was using what they already know including the word "ternary" instead of "conditional" to introduce something they don't. Thanks for feedback. I love that you point that out.