TypeScript: Any | Unknown | Never
Tags: typescript, javascript, webdev
Published: 2021-09-04 08:19:27

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.

{% link https://dev.to/ponikar/introduction-to-typescript-1acj %}


So let's get started.

![Typescript any never unknown](https://media.giphy.com/media/oS8pRFxbD0d44/giphy.gif)

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.** 

```typescript
let iAmCrazy: any = "I really am crazy";
iAmCrazy.map(crazy => crazy.any);
iAmCrazy.anyValue = "HEHE";
iAmCrazy.as.i.can.be();
```

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.

```typescript
let num :unknown;
```

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
```typescript
let num :unknown;

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

```

> 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.** 

```javascript

const Greeting = () => {
  console.log("Hello world");
}
console.log(Greeting());
``` 
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.

```javascript
const Greeting = () :never => {
  console.log("Hello world");
}
```
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,
```typescript 
// When function throws error
const iWillThrowError = ():never => {
    throw new Error("BOOM");
}
```

```typescript
// when we use something like infinity loop 
const infinite = () :never => {
    while(1) {
       console.log("How you doing?");
    }
}
```

**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,