By Ashish Lahoti and dmitripavlutin

Have a dream, have dry goods, wechat search [big Move the world] pay attention to this in the early morning is still in the bowl washing wisdom.

In this paper, making github.com/qq449245884… Has been included, a line of large factory interview complete test sites, information and my series of articles.

We know that variables of type any can be assigned to any value.

let myVar: any = 0;
myVar = '1';
myVar = false;
Copy the code

The TypeScript guide discourages the use of any because using it would throw away type restrictions — and the need for type restrictions is one of the reasons we chose TypeScript, so it’s kind of the opposite.

TypeScript (3.0 and above) also provides a special type, unknown, similar to any. We can also assign any value to a variable of unknown type:

let myVar: unknown = 0;
myVar = '1';
myVar = false;
Copy the code

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

We set the sole argument to invokeAnything() to type any

function invokeAnything(callback: any) {
  callback();
}

invokeAnything(1); // throws "TypeError: callback is not a function"
Copy the code

Because the callback argument is of any type, the statement callback() does not trigger a type error. We can do anything with a variable of type any.

However, the runtime throws a runtime error :TypeError: Callback is not a function. 1 is a number that can’t be called as a function, and TypeScript doesn’t protect your code from this error

How do you allow the invokeAnything() function to accept arguments of any type, but also force a type check on that argument to prevent errors like the one above?

Please welcomeunknownBig brother controls the field.

Like any, unknown variables accept any value. However, TypeScript enforces type checking when trying to use unknown variables. That’s what we want.

function invokeAnything(callback: unknown) {
  callback();
  // Object is of type 'unknown'
}

invokeAnything(1);
Copy the code

The callback() statement has a type error :Object is of type ‘unknown’. In contrast to any, TypeScript protects us from calling things that might not be functions.

Before using a variable of unknown type, you need to do a type check. In this case, we just need to check whether the callback is a function type.

function invokeAnything(callback: unknown) {
  if (typeof callback === 'function') {
    callback();
  }
}

invokeAnything(1);
Copy the code

2. Mental models of Unknown and Any

To be honest, when I study, I have a hard time understanding Unknown. How is it different from any because both types accept any value

Here are some rules that help me understand the difference:

  • You can assign anything to the unknown type, but you cannot operate on unknown until you have done type checking or type assertion

  • You can assign anything to any type, and you can do anything to any type

The above example illustrates the similarities and differences between unknown and ‘any’.

Unknown sample:

function invokeAnything(callback: Unknown) {// You can assign anything to the type 'unknown', // But before type checking or type assertion, If (typeof callback === 'function') {callback(); } } invokeAnything(1); // You can assign anything to `unknown` typeCopy the code

Type checks typeof callback === ‘function’ to see if the callback is a function and, if so, can be called.

Any sample:

Function invokeAnything(callback: any) {function invokeAnything(callback: any) {function invokeAnything(callback: any) { } invokeAnything(1); // You can assign anything to type 'any'Copy the code

If callback is any, TypeScript does not enforce any type checking for the callback() statement.

3. Summary

Unknown and any are two special types that can hold any value.

It is recommended to use unknown rather than any because it provides a more secure type — if you want to operate on unknown, you must use type assertion or narrow down to a specific type.

~~ finish, I am small wisdom, female ticket is teaching training industry work, the recent salary is a little low, I prepare to work more sea, earn more money.


The possible bugs in editing can not be known in real time. In order to solve these bugs after the event, I spent a lot of time on log debugging. By the way, I recommend a good bug monitoring tool for youFundebug.

Original text: dmitripvlutin.com/typescript-…

communication

Have a dream, have dry goods, wechat search [big Move the world] pay attention to this in the early morning is still in the bowl washing wisdom.

In this paper, making github.com/qq449245884… Has been included, a line of large factory interview complete test sites, information and my series of articles.