Typescript any vs unknown vs never

TypeScript: Any vs Unknown vs Never

What is the difference between any , unknown , and never ? When should we be using each one of those? What are its drawbacks and strengths?

TypeScript has recently turned ten years. To celebrate its anniversary, I wanted to explore and play with the intricacies of those types.

This article should help you understand and learn their internals. By having that crystal clear in your mind you will easily identify which one should you use in each situation.

Out of those three, the any is the one we tend to learn first. You can think of it as an escape hatch of the TypeScript type system. For example, when declaring a variable as any you can assign the value to any value to it. It is a compiler directive to disable TypeScript and shift the responsibility to the developer.

More often than not, it will create more issues than it will solve.

By default, when the type annotation is missing and can’t be inferred, the compiler will default to the any type.

This is considered a bad practice and has an easy fix. We can enable strict mode on to make the above code fail.

TypeScript strict mode will enable noImplicitAny , noImplicitThis , alwaysStrict, strictBindCallApply , strictNullChecks , strictFunctionTypes , and strictPropertyInitialization under the hood.

The noImplicitAny will raise an error when implicit usages of the any type. Instead of enabling strict you may choose to enable that one only.

The usage of explicit any will not be banned though. That can lead to some nasty errors.

Читайте также:  overflow

Источник

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.

ponikar

Introduction to TypeScript

Ponikar ・ Jun 21 ・ 3 min read

So let’s get started. 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.

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?

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

let num :unknown; if(typeof num === "string")  // do something for string > else if(typeof num === "number")  // do something for number > 

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

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()); 

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"); > 

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"); > 
// 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,

Источник

Оцените статью