Check out a free preview of the full Intermediate TypeScript, v2 course

The "Unit Types" Lesson is part of the full, Intermediate TypeScript, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Mike explains that unit types are types that can only have one value, such as null and undefined. Suggestions to avoid creating variables of type void and that literal types can also be used to create unit types are also covered in this segment.


Transcript from the "Unit Types" Lesson

>> The last little topic we're gonna talk about is unit types. Unit types, this term describes types that could be only one value. JavaScript, and as a consequence, TypeScript, they have a couple of these. Null is a unit type, there's only one null. Every time you're writing null you're referring to the same thing.

Undefined is the same way. You could see these are not interchangeable given that I have strict null checks turned on. If I had it turned off, this would pass. Wait, this would pass, the second one. Cuz it would say, all right, undefined can accept a null. I don't think a null type could still accept an undefined in that case.

So one more is void. This is not a unit type, actually. It's almost a unit type. Void is what you get from a function that does not return. It is an indication that you shouldn't care about what something returns. I know those are two different statements. In TypeScript Fundamentals when we talk about typing functions, this is in the constructibles and callable section.

We talked about how void with a function declaration, that's one thing. But if you're using it to type a callback, you could put something in there that does return something, but it should be ignored. So if you need a refresher on that, go look at that chapter. The important thing here is, all right, we've got myVoid, it's of type void.

And it's not only willing to accept voids, but it's willing to accept undefinds. This is because at runtime, these will actually be undefineds. It doesn't accept any other value. Voids can accept voids, but they can also accept undefineds. Don't create variables of type void, this is just here for completeness.

And if you're creating a variable of type void, it's a smell that something's wrong. You have stored the result of a function that does not want you to store its result. Yes, sir.
>> When we were using a type that was one single number, would that also be considered a unit type or a string of the letter A?

>> It would be, it would be, and that's absolutely true. You could create a unit type just like this. Right, this is exactly the number 27. The reason I didn't add it to this list is we don't have something that ships with Typescript that is just the number 27.

You can create a unit type with a literal type, but I kind of wanted to cover mostly these things that we work with, like undefined and null, they have special names. They're not something you have to explicitly create. They already are in TypeScript. But you're absolutely right, this is a unit type.

Only one thing. I mean, and if you were to do this, Now it's not a unit type anymore. It could be two things. [LAUGH] sorry, this is wrong, you get what I mean, though. I'd have to flip a coin here or something.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now