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.

Get Unlimited Access Now

Transcript from the "Unit Types" Lesson

[00:00:00]
>> 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.

[00:00:21] 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.

[00:00:49] 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.

[00:01:18] 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.

[00:01:38] 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.

[00:02:02] 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?

[00:02:24]
>> 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.

[00:02:48] 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.

[00:03:08] 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.