The Weird Parts of TypeScript 13 exercises

Accept Anything Except Null or Undefined

Here we have a function acceptAnythingExceptNullOrUndefined that hasn't been assigned a type annotation yet:

const acceptAnythingExceptNullOrUndefined = (input) => {}; // red squiggly line under input

This function can be called with a variety of inputs: strings, numbers, boole

Loading exercise


00:00 In this exercise, we're going to look at a really interesting new type. We have a function here called AcceptAnythingExceptNullOrUndefined where the input has not been given a type annotation yet. And as you can see, we are basically calling this function with a bunch of different stuff.

00:15 With strings, with a number, with true, with a symbol, with object, with an array, with a function, with a regex, and with a class, with like an error. And we're expecting it to basically be absolutely fine in all of these cases,

00:32 but error if we pass it null or undefined. This is a quite a hard type to define, and when you find the definition, your brain's going to explode. I think it's extremely simple, and it's very key to understanding some of the deeper parts of TypeScript. I will link to an article below that explains more about it. Good luck!