Conditional Types and Infer 10 exercises
solution

# Compare and Return Values with Extends and the Ternary Operatorself.__wrap_balancer=(t,e,n)=>{n=n||document.querySelector(`[data-br="\${t}"]`);let o=n.parentElement,r=E=>n.style.maxWidth=E+"px";n.style.maxWidth="";let i=o.clientWidth,s=o.clientHeight,c=i/2,u=i,d;if(i){for(;c+1<u;)d=~~((c+u)/2),r(d),o.clientHeight==s?u=d:c=d;r(u*e+i*(1-e))}};self.__wrap_balancer(":Rhd9r6:",1)

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

## 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.