Conditional Types and Infer 10 exercises
Problem

Refine Conditional Logic in a Type Helper

The starting point is our solution from the last exercise:

type YouSayGoodbyeAndISayHello<T> = T extends "hello" ? "goodbye" : "hello"; 

Challenge

There's one more thing to add to this code. Recall that if we pass something in that is not "hello" that we will be returned "hello."

Y

Loading exercise

Transcript

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.