‘any’ vs ‘unknown’ in TypeScript ?

When you start learning TypeScript, you will come across two types – any and unknown.
Today, I will try to explain the difference between the two in the simplest way possible.

any – The any type allows us to assign literally “any” particular value to …


This content originally appeared on DEV Community and was authored by Karishma

When you start learning TypeScript, you will come across two types - any and unknown.
Today, I will try to explain the difference between the two in the simplest way possible.

any - The any type allows us to assign literally “any” particular value to that variable, simulating what we know as plain JavaScript.

unknown - The unknown type is the type-safe counterpart of any. Anything is assignable to unknown, but unknown isn't assignable to anything but itself and any without a type assertion or a control flow based narrowing.

Errghh... Let's understand with an example.

let age: number;
let userAge: any;

userAge = 'This is some age';
userAge = 20;

age = userAge;

And yes, This code works! ? Type of userAge is any so it can be assigned any value - string, number etc.

let age: number;
let userAge: unknown;

userAge = 'This is some age';
userAge = 20;

age = userAge;

The statement age=userAge gives an error. I mean, the type is unknown so what is the problem here? To assign an unknown value to a value with a fixed type, we have to do some quick type check!

let age: number;
let userAge: unknown;

userAge = 'This is some age';
userAge = 20;

if(typeof userAge === 'number') {
  age = userAge;
}

And now this works too! ?

When to use what? ?
Honestly speaking you shouldn't use either of them. But if you really really have to then unknown is a better choice if you know what you want to do with that value eventually.
I don't recommend using any - it takes away the actual essence of TypeScript!

I hope you learnt at least something. Also, this was my first ever post on Dev! Thank you for reading :)


This content originally appeared on DEV Community and was authored by Karishma


Print Share Comment Cite Upload Translate Updates
APA

Karishma | Sciencx (2021-07-05T19:50:08+00:00) ‘any’ vs ‘unknown’ in TypeScript ?. Retrieved from https://www.scien.cx/2021/07/05/any-vs-unknown-in-typescript-%f0%9f%91%80/

MLA
" » ‘any’ vs ‘unknown’ in TypeScript ?." Karishma | Sciencx - Monday July 5, 2021, https://www.scien.cx/2021/07/05/any-vs-unknown-in-typescript-%f0%9f%91%80/
HARVARD
Karishma | Sciencx Monday July 5, 2021 » ‘any’ vs ‘unknown’ in TypeScript ?., viewed ,<https://www.scien.cx/2021/07/05/any-vs-unknown-in-typescript-%f0%9f%91%80/>
VANCOUVER
Karishma | Sciencx - » ‘any’ vs ‘unknown’ in TypeScript ?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/05/any-vs-unknown-in-typescript-%f0%9f%91%80/
CHICAGO
" » ‘any’ vs ‘unknown’ in TypeScript ?." Karishma | Sciencx - Accessed . https://www.scien.cx/2021/07/05/any-vs-unknown-in-typescript-%f0%9f%91%80/
IEEE
" » ‘any’ vs ‘unknown’ in TypeScript ?." Karishma | Sciencx [Online]. Available: https://www.scien.cx/2021/07/05/any-vs-unknown-in-typescript-%f0%9f%91%80/. [Accessed: ]
rf:citation
» ‘any’ vs ‘unknown’ in TypeScript ? | Karishma | Sciencx | https://www.scien.cx/2021/07/05/any-vs-unknown-in-typescript-%f0%9f%91%80/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.