Type Transformations Workshop (9 exercises)

Refine Conditional Logic in a Type Helper

There's one more thing to add on to our conditional type exercise.

The starting point is our solution from the last exercise:

Recall that if we pass something in that is not "hello" that we will be returned "hello."


Your challenge is to update the conditional type to handle cases where T extends neither "hello" or "goodbye".

Think of it as saying we never want something that isn't hello or goodbye to be passed in.

Hint: check out the TypeScript docs on never and be ready to nest some ternaries.


[0:00] In this exercise, we extend the conditional type problem from last time, which is we have this YouSayGoodbyeAndISayHello, except we now want to handle the cases where you don't pass in either hello or goodbye.
[0:15] We're going to handle them in a specific way, where we want to check, first of all, if T extends hello or goodbye. If it does, then allow this check to be performed and return the correct thing. Otherwise, we're going to ask it to return never.

[0:32] I'm going to explain more about the never type in the solution. Really, never is a way of saying this should never happen. It's very useful in conditional types, a very common pattern to basically say, "OK, this value isn't allowed," or "This should never happen."

[0:46] That's your challenge. You may need to nest some ternaries in here. The syntax is going to be looking a little bit weird by the time you're finished, but I promise, even if it does look strange, if you get all of these solutions passing, you're probably doing something right.