Conditional Types and Infer 10 exercises
solution

Compare and Return Values with Extends and the Ternary Operator

The first thing we need to do is add a type argument to YouSayGoodbyeAndISayHello to turn it from a static value into a function.


type YouSayGoodbyeAndISayHello<T>

Now that we're receiving the T, we add a check to see if it extends hello. If it does, return goodbye. Otherwise, re

Loading solution

Transcript

0:00 This type now, this type helper, YouSayGoodbyeAndISayHello, the first thing that we need to do is add a type argument to it. This type argument turns this from a static value into a function. Now, this T is the thing that we're receiving. We do a check here.

0:19 This extends keyword, we're going to talk about this a lot, but this might be one of the first times that you're seeing this in the course. If it's the first or second time, then what this is doing here is we're checking if T extends hello, if it can be compared to hello. If it can be compared to hello, then we return goodbye. Otherwise, we return hello.

0:43 Let's say, "type Example." We say, "YouSayGoodbyeAndISayHello hello." What we get back is goodbye because we've triggered this branch. It's passing this check here.

0:55 If we put in something random here, then does it extend hello? Does it match that pattern? No, it doesn't, so we end up with hello as the return type. We can change this to blah, blah, blah, blah, blah. We'll get back blah, blah, blah, blah, blah.

1:11 This is really intriguing. This lets us do really complex type logic within this function structure. We can pass in different things and return different things based on input. This is going to feel pretty overwhelming right now, but this drives a lot of the really, really powerful stuff you can do with TypeScript.

1:33 This conditional type here is immensely powerful. It starts to turn TypeScript from just some annotations that you can add to your JavaScript to its own meta programming language itself. This is what starts creating the Turing completeness of TypeScript that you might have heard about.

1:53 This very simple concept is really integral to what makes TypeScript powerful. Yes, unfortunately, the only way to do this right now, as of TypeScript 4.8, is to do this ternary syntax. You can nest these ternaries. We may look at this later. It is pretty ugly to look at.

2:14 Hopefully, you've understood all of the constituent parts here. We have a type function with the arguments here, with the check here, with the if-true and the if-false here.