Check out a free preview of the full TypeScript 5+ Fundamentals, v4 course

The "void Type" Lesson is part of the full, TypeScript 5+ Fundamentals, v4 course featured in this preview video. Here's what you'd learn in this lesson:

Mike explains how "void" is used to indicate that a function does not have a return value or that the return value should be ignored. The difference between "void" and "undefined" and examples to illustrate the usefulness of "void" in certain scenarios are also covered in this segment.

Preview
Close

Transcript from the "void Type" Lesson

[00:00:00]
>> While we're talking about callables, let's talk about void. So if we look at this function, look at its return type, its void. If you've gotten really deep into Javascript, you may know that there is a void keyword in Javascript It means, in this context, ignore it. It literally means do not consume the return value of this function.

[00:00:33]
It is not what we would call a unit type as null and undefined are for one reason, Don't do this. So you could do this. Technically correct. So it accepts a void. It also accepts an undefined. Can anyone think of why would it be important that void accepts an undefined?

[00:01:05]
>> That would functions actually return when there's no return.
>> That is exactly right. It's what functions return when there is no return. So a void can be, the type void is itself and undefined. There are exactly two things that are allowed in there. Whereas null, or if we were to do undefined here, or if we were to do, yeah, null, undefined is not gonna do.

[00:01:39]
This is not gonna do Only this will do we would call that a unit type like those literal types right it's a set with one thing in it so. Let's look at these two examples here as an illustration of why it's useful to have void. This kind of demonstrates the importance of void's existence.

[00:02:09]
So we've got two functions here. We've got InvokeInFourSeconds and InvokeInFiveSeconds. They're very similar. This is the only change between them, so don't stare at them too closely. We're going to create an array of values. And here we're going to say invoke in four seconds. And then we're going to have pass in an arrow function values.push 4.

[00:02:30]
If we do the same exact thing for invoking five seconds, it type checks And here's why. Most people don't think about this too much, but push returns a number. It actually returns a number. And so what we'd be saying here is, I'm actually returning something. And it's not undefined.

[00:02:59]
It is not the specific value undefined, like as if I was doing this. I am not returning an undefined. I am returning a number. Here I'm also returning a number, but you know what we're saying. Don't worry about it. I don't care what the return is. That's where you would use a void in place of an undefined.

[00:03:29]
Do not create variables of type void. It really only has meaning in the context of the return value of a function, but it does have a purpose here. I mean, you could also do this, but then of course, like within this function you could start to consume the value of this callback and types your would be perfectly happy to let you do so.

[00:03:56]
When it's void, you won't get anywhere trying that, which is what you want, right? You want a helpful constraint there. Yep, LJ, you're right, push returns the length of the array. Is it after the push? Returns the new length, yep. Last question in the chat, so we use void when we don't care about the return value irrespective of a value being returned or not, correct.

[00:04:22]
That's correct. Void means ignore my return value. And one caveat here, if you do this You'll be busted on this so on the one hand we've we have a callback here I put this back this is saying I accept a function with any return type and please ignore it up here, we're saying, I'm a function that should not return, or I think I can still do this.

[00:04:59]
Can I do this? Yeah, I can do that too. But like literally anything else, I will be stopped from doing. So it's sorta a has a similar meaning but slightly different depending on what you're using where you're defining the return type if it is a function declaration,right remember this is a value to value of type function with a return type.

[00:05:27]
So when it's used as the return type of a function declaration it means I should follow through on a commitment to return nothing useful. At best, I return undefined. Either I'll create an undefined which is weird. Or I'll just write return or I won't have anything at all like this function right now.

[00:05:51]
Like it's just pure side effects. And it returns when you reach the end of the function. Here, it's perfectly willing to accept a return value, but it makes sure that within the function that this callback is used, you're not doing anything with the return value. A little bit of nuance there but they kind of both mean I shouldn't return anything interesting, or whatever I'm returning is not interesting that's void.

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