DEV Community

Cover image for TypeScript: Any | Unknown | Never
Ponikar
Ponikar

Posted on • Updated on

TypeScript: Any | Unknown | Never

Hello there, Today we are going to discuss about type annotations which is provided by TypeScript itself.

Also Checkout my recent blog about Introduction to TypeScript. If you want to know more about it.

So let's get started.

Typescript any never unknown

As we know that Typescript provide static type safety that ensures that we don't have any potentials type bug in production but sometimes it's kind of hard to determine the type at compile time.

That's is why TypeScript provide defaults Type Annotation to make sure Type Safety.

1. Any

As a name suggest this provide type any. It's universal. Basically you can assign any type of value.

Use any as type annotation is bad practice.

If you are using any as type annotation then there is no sense to use typescript.

let iAmCrazy: any = "I really am crazy";
iAmCrazy.map(crazy => crazy.any);
iAmCrazy.anyValue = "HEHE";
iAmCrazy.as.i.can.be();
Enter fullscreen mode Exit fullscreen mode

above block of codes won't yell at you. Because of any.

Avoid any as long as possible.

But As beginner, There is chance that you might end up using any at some cases. So how to avoid use of any in those cases?

that's why we have unknown.

2. Unknown

When you are not sure about what type you can assign? Use unknown.

let num :unknown;
Enter fullscreen mode Exit fullscreen mode

Hypothetically user can pass number like this "232" or 232.
There are two possibilities of num type. It can either be string or number.

You can do something like this

let num :unknown;

if(typeof num === "string") {
   // do something for string 
} else if(typeof num === "number") {
  // do something for number
}

Enter fullscreen mode Exit fullscreen mode

You can definitely use union type but in order to explain unknown I am using if else block.

So that's how unknown works. When you are not sure about the type. You can assign unknown and you can check the type at runtime like I did in above codeblock.

3. Never

In a simple words, never gives you hint that this will never occur at all.

Basically never use with functions.

In JavaScript, We know that if your function is not returning anything, it's still return undefined.


const Greeting = () => {
  console.log("Hello world");
}
console.log(Greeting());
Enter fullscreen mode Exit fullscreen mode

if you run the above code, you will see that the last console.log statement will give you undefined.

Now if you assign never to function, this will give you error.

const Greeting = () :never => {
  console.log("Hello world");
}
Enter fullscreen mode Exit fullscreen mode

This will give you error because our function is still returning undefined.

When your function is not returning anything or your function is not reaching to the end of code block, you can assign never to that. This will help other developers to understand that this function execution will never end.

In some cases like,

// When function throws error
const iWillThrowError = ():never => {
    throw new Error("BOOM");
}
Enter fullscreen mode Exit fullscreen mode
// when we use something like infinity loop 
const infinite = () :never => {
    while(1) {
       console.log("How you doing?");
    }
}
Enter fullscreen mode Exit fullscreen mode

You are not often going to use never. But now you know if a function has never return type, that's means the execution of that function will never end. So It will be easy to handle those type of cases.

So that's it. It was all about this 3 type annotations. It's really important to understand, When to use?

I hope this blog would be helpful to you to understand about any | unknown | never!

If you think there is something missing or wrong, Feel free to share your thoughts in comments.

Keep Learning and Growing!
Thank you,

Top comments (1)

Collapse
 
ponikar profile image
Ponikar

one of the best comment that I have ever read. Thank you for sharing!