In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the difference between unknown and any in TypeScript". In daily operation, I believe that many people have doubts about the difference between unknown and any in TypeScript. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "what is the difference between unknown and any in TypeScript?" Next, please follow the editor to study!
We know that variables of type any can be assigned to any value.
Let myVar: any = 0% myVar ='1%% MyVar = false
The TypeScript guidelines don't encourage the use of any because you lose type restrictions-- and the need for type restrictions is one of the reasons we chose TypeScript, so it's just a bit of the opposite.
TypeScript (version 3. 0 and above) also provides a special type of unknown similar to any. We can also assign any value to the unknown type variable:
Let myVar: unknown = 0% myVar ='1%% MyVar = false
So now there's a question. What's the difference between any and unknown?
1. Unknown vs any
To better understand the difference between unknown and any, let's start by writing a function that wants to call its unique parameters.
We set the only parameter of invokeAnything () to type any
Function invokeAnything (callback: any) {callback ();} invokeAnything (1); / / throws "TypeError: callback is not a function"
Because the callback parameter is of any type, the statement callback () does not trigger a type error. We can do anything with variables of type any.
But the run throws a runtime error: TypeError: callback is not a function. 1 is a number and cannot be called as a function. TypeScript does not protect the code from this error.
What should I do to allow the invokeAnything () function to accept any type of parameter while forcing a type check on that parameter to prevent such an error?
Brother unknown is invited to control the game.
Like any, the unknown variable accepts any value. But when you try to use the unknown variable, TypeScript enforces type checking. That's what we want.
Function invokeAnything (callback: unknown) {callback (); / / Object is of type 'unknown'} invokeAnything (1)
Because the type of the callback parameter is unknown, the statement callback () has a type error: Object is of type 'unknown'. In contrast to any, TypeScript protects us from calling things that may not be functions.
You need to do type checking before using a variable of type unknown. In this example, we just need to check whether callback is a function type.
Function invokeAnything (callback: unknown) {if (typeof callback = 'function') {callback ();}} invokeAnything (1); 2. Mental models of unknown and any
To tell you the truth, when I am studying, it is difficult for me to understand unknown. It is different from any because both types accept any value
Here are the rules that help me understand the difference between the two:
You can assign anything to a unknown type, but you cannot operate on unknown before type checking or type assertion
You can assign anything to the any type, or you can do anything with the any type
The above example illustrates the similarities and differences between unknown and `any.
Unknown example:
Function invokeAnything (callback: unknown) {/ / you can assign anything to the type `unknown`, / / but you cannot operate on `unknown` if (typeof callback = = 'function') {callback ();}} invokeAnything (1); / / You can assign anything to `unknown` type before type checking or type assertion
Type check typeof callback = = 'function', checks whether callback is a function, and if so, it can be called.
Any example:
Function invokeAnything (callback: any) {/ / you can perform any operation on the `any` type callback ();} invokeAnything (1); / / you can assign anything to the `any` type
If callback is any, TypeScript does not force any type checking on the callback () statement.
At this point, the study on "what is the difference between unknown and any in TypeScript" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.