any, unknown, never and void in TypeScript

any

any means any type .

It is the parent class of any type, and any type of value can be assigned to any type:

// 编译不会报错
let anything: any = '前端西瓜哥';

let flag: boolean = true;
anything = flag;

anything = { num: 2 };

It can represent any type and use their syntax just like writing native JS without types.

// 编译不会报错
const a: any = 6;

a();
a.key1 = true;

Any is equivalent to abandoning the type system, which will make the code unpredictable and difficult to maintain. Programmers need to be careful to maintain it. If they are not careful, it will cause runtime errors, so use it as little as possible.

But when introducing some third-party pure JS libraries that do not provide types, you still have to mark them as any. There is really no way around this.

Unless you write a type declaration for the third-party library yourself, it is too unrealistic because you are not familiar with the API of the third-party library and may have very complex type derivation to implement.

any exists for compatibility with untyped JS. TS is a superset of JS, and it is necessary to use any to open a backdoor.

unknown

unknown can be considered a more type-safe version of any .

Like any, unknown is also a subtype of any type, and all types can be passed to unknown, including any.

// 编译不会报错
let a: unknown = '前端西瓜哥';

let b: any;
a = b;

a = { num: 2 };

copy

It is said that unknown is safer because unknown cannot perform any operations . If you want to use it, you need to use as for explicit type assertion.

never

never represents a type that cannot be observed , and variables assigned to this type can do nothing.

Some scenarios where never is used.

(1) A function that cannot reach the return value, for example, will definitely throw an error or an infinite loop:

// 这里的 never 表示无法执行到函数返回它的返回值
function foo(): never {
  throw new Error('something wrong!')
}

(2) TS will narrow the type under judgment conditions. When the type shrinks to the point where no type is available, the type becomes never.

void

void is used to indicate that a function has no return value .

function sayHi(): void {
  console.log('Hi!');
}

copy

Of course, when the actual JS is run, a default undefined will still be returned. But TS sets the return value to void, which has better semantics.

Summarize

any is any type, has all types of behavior, can be executed, has access to properties, and is outside the type system.

unknown is a safer type of any. You can also assign any type to it, but you cannot perform any operations. You must use a type assertion to display the type before you can perform operations.

never is an unobservable type, such as the return value of a function that will not be executed, and the intersection type whose merge result does not exist. Very active in type programming, often used to discard some subtypes.

Void is relatively simple, it just means that the function has no return value, and there are no other scenarios.

any, unknown, never and void in TypeScript-Tencent Cloud Developer Community-Tencent Cloud

 

 

Guess you like

Origin blog.csdn.net/zdwzzu2006/article/details/132845756