Any Never Void Unknown in Typescript
Any Never Void Unknown in Typescript

Hello and welcome back to the channel. In this video you will learn about most needed types in Typescript like void, any, never and unknown. Also as a bonus I will cover type assertion feature so make sure that you watch it until the end.

Let's start with void because it's the easiest to understand. Normally we use void only with functions when we want to define that our function just do something and doesn't return any value.

const doSomething = (): void => {

As you can see I wrote void as a return type because our function doesn't return any type.

By the way if we don't define void at doSomething function Typescript will still guess it. But I recommend always to define it explicitly. In this case if you changed the logic inside and returned something you will get an error immediately.

Also it doesn't make any sense to create a variable with void type just because we can assign only null and undefined inside and it doesn't bring us anything.

let a: void = null

So normally we are using void only to define that we don't have a returned value inside a function.

Now let's talk about any. It a type which means that inside it can be any type. Any simply means that you turn of Typescript support in this specific place and you don't have any type check ever.

And this is actually the worst thing in Typescript that exists. Because it's a silver bullet for everything. When people start writing Typescript and they can't fix some problem or error with types they always just write everywhere any. The problem is that your project then looks like a house with holes everywhere. This is why I highly recommend you to never use any and always try to fix problem correctly by resolving Typescript errors. Because Typescript gives you errors not because it's a dumb tool but to avoid runtime errors. So yes it might be that you need any type when you are writing some generic code but normally it's like 5 times in a huge project. If you have any 20 times or more in your project then you are using Typescript in a wrong way.

The next type that I want to show you in never. It's much more rare thing and you won't use it a lot. It just says that the value will never happen. So if we have a function which always just throws an error than we can return never there because there is no value returned.

const doSomething = (): never => {

const doSomething = (): void => {
 throw 'never'

As you can see we can't use never if function is fully readable by Typescript. There are more advanced cases of using never but I don't want to go too deep here.

The last type that I want to cover is unknown. It was introduced in Typescript 3 and is a safer alternative to any. So let's check on the example to understand the difference better.

let vAny: any = 10; // We can assign anthing to any
let vUnknown: unknown = 10; // We can assign anthing to unknown just like any

let s1: string = vAny; // Any is assigable to anything
let s2: string = vUnknown; // Invalid we can't assign vUnknown to any other type (without an explicit assertion)

vAny.method(); // ok anything goes with any
vUnknown.method(); // not ok, we don't know anything about this variable

At the beginning there is no difference between any and unknown we can assign anything to the property with this type. But when we try to use it and assign to other property for example unknown won't allow us to do this because the type is unknown and Typescript waits until we somehow narrow the type to the type that is needed. Any doesn't care about anything at all.

The same goes about calling some methods from the variable. Any doesn't validate it at all but unknown won't allow us to do it because we didn't define yet what is the correct type.

And the question that you might ask here is how we can define a type in this case?

So here is a bonus regarding type assertions. It is possible to tell Typescript to treat the type of the variable somehow different. We are using as keyword for this.

let pageNumber: string = "1";
let numericPageNumber: number = pageNumber as number;

This is how we can tell Typescript that it should treat pageNumber as a number and not a string. It can also lead to bad code if you lie to Typescript regarding the real type. But as you can see we are getting an error here. Typescript this that we made a mistake unintentionally.

Conversion of type 'string' to type 'number' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.

So what we need to do here is to convert string first to unknown and then to the type that we need to use.

let pageNumber: string = "1";
let numericPageNumber: number = (pageNumber as unknown) as number;

Now it works correct. First we said Typescript that type is unknown and then that it's a number. The same type assertion we need to use if we have unknown property.

let vUnknown: unknown = 10;
let s2: string = vUnknown as string;

So we successfully learned about such important types as void any never and unknown. I recommend to use void and unknown. I almost never need to use never type and it's better if you forget that any exists at all.

Also if you want to improve your programming skill I have lots of full courses regarding different web technologies.

📚 Source code of what we've done