What is the difference between unknown and any in TypeScript

前端小智
中文
Author: Ashish Lahoti
Translator: Frontend Xiaozhi
Source: dmitripavlutin

If you have dreams and dry goods, search for [Moving to the World] Follow this wise brush who is still doing dishes in the early morning.

This article GitHub https://github.com/qq449245884/xiaozhi has been included, the first-line interview complete test site, information and my series of articles.

We know that any can be assigned to any value.

let myVar: any = 0;
myVar = '1';
myVar = false;

TypeScript guide does not encourage the use of any , because using it will lose type restrictions-and the need for type restrictions is also one of the reasons we choose TypeScript, so it is a bit contrary.

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

let myVar: unknown = 0;
myVar = '1';
myVar = false;

Now there is a question, what is the difference between any and unknown

1. unknown vs any

In order to better understand unknown and any , let's start by writing a function that wants to call its only parameter.

We will invokeAnything() only parameter is set to any type

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() will not trigger a type error. We can do anything with variables of type any

But the operation will throw 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 to avoid this error

invokeAnything() function is allowed to accept any type of parameter, and the type check of the parameter is forced to prevent the above error?

unknown brother 06179f61a8746c to control the field.

Like any , the unknown variable accepts any value. But when trying to use the unknown variable, TypeScript enforces type checking. Isn't that what we want.

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

invokeAnything(1);

Because the type of the parameter callback unknown , the statement callback() has a type error: Object is of type 'unknown' . Contrary to any , TypeScript protects us from calling things that might not be functions.

Before using a unknown type 06179f61a874fe, you need to perform a type check. In this example, we only need to check callback is a function type.

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

invokeAnything(1);

2. The mental models of unknown and any

To be honest, when I was studying, I had a hard time understanding unknown . any is it different from 06179f61a87571, because both types accept any value

Here are the rules that helped me understand the difference between the two:

  • You can assign anything to the unknown type, but you cannot perform operations unknown before performing type checking or type assertion
  • You can assign anything to the any type, and you can also perform any operation any

The above example just illustrates the similarities and differences between unknown

unknown example:

function invokeAnything(callback: unknown) {
  // 可以将任何东西赋给 `unknown` 类型,
  // 但在进行类型检查或类型断言之前,不能对 `unknown` 进行操作
  if (typeof callback === 'function') {
    callback();
  }
}

invokeAnything(1); // You can assign anything to `unknown` type

Type check typeof callback === 'function' , check callback is a function, if it is, you can call it.

any example:

function invokeAnything(callback: any) {
  // 可以对 `any` 类型执行任何操作
  callback();
}

invokeAnything(1); // 可以把任何东西分配给`any`类型

If callback is any , TypeScript will not force the callback() statement to perform any type checking.

3. Summary

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

It is recommended to use unknown instead of any because it provides a safer type-if you want to unknown , you must use type assertion or narrow it down to a specific type.

~~ At the end, I am Xiaozhi. The female ticket works in the education and training industry. The salary paid recently is a bit low. I am going to work more and earn more money.


possible bugs in 16179f61a878b9 editing cannot be known in real time. In order to solve these bugs afterwards, a lot of time was spent on log debugging. By the way, I would like to recommend a useful BUG monitoring tool Fundebug .

Original: https://dmitripvlutin.com/typescript-unknown-vs-any/

comminicate

If you have dreams and dry goods, search on [Daily Move to the World] Follow this brushing wit who is still doing dishes in the early morning.

This article GitHub https://github.com/qq449245884/xiaozhi has been included, the first-line factory interview complete test sites, materials and my series of articles.

阅读 538

终身学习者
我要先坚持分享20年,大家来一起见证吧。
62.7k 声望
94.2k 粉丝
0 条评论
62.7k 声望
94.2k 粉丝
文章目录
宣传栏