Type Helpers 9 exercises

Constraining Types for Anything but null or undefined

As a thought experiment, your challenge is to update Maybe to have a constraint that will not allow null or undefined to be passed in.

You'll know you have it right when @ts-expect-error lines in the tests no long show an error:

type tests = [
// @ts-expect-error

Loading exercise


0:01 For this exercise, we're going to be doing something interesting. This is more of a thought experiment. We have this Maybe type here, which we've seen before. It's T, or T, or null, or undefined.

0:11 What we're doing here is we're saying, we can pass in Maybe string, Maybe false, Maybe zero, Maybe this to an empty string. What we want to do is actually exclude null or undefined from this T here.

0:25 In other words, we want to find a constraint that basically means we want this to be anything other than null or undefined. I'm asking you this is for a reason, because it hones in on something really important conceptually about typescript itself. Good luck.