[ts] typeScript advanced: any and unknown

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right


Recommended website for essential front-end tools (free picture bed, API, ChatAI and other practical tools):
http://luckycola.com.cn/

Preface

Learning objectives:
1. The difference between any and unknown
2. Union types and intersection types between unknown types and other types
3. Application of unknown – union type contraction


提示:以下是本篇文章正文内容,下面案例可供参考

1. The difference between any and unknown types

1. The any type represents any type. The ts type check is abandoned and should be used less in ts.

type T1 = keyof any; // string | number | symbol

2. The unkonw type is a temporarily unknown type (will be known later), and ts type checking will still be performed.

type T2 = keyof unknown;// never

3. In the ts type system, never (empty type) is the narrowest type, and any is the widest type.

2. Union types and intersection types between unknown types and other types

1. Union type of unknown type and other types

The code is as follows (example):

type T00 = unknown | null; // unknown
type T00 = unknown | undefined; // unknown
type T00 = unknown | null | undefined; // unknown
type T00 = unknown | string; // unknown
type T00 = unknown | string[]; // unknown
type T00 = unknown | any; // any
type T00 = unknown | unknown; // unknown

2. Intersection types between unknown types and other types

The code is as follows (example):

type T00 = unknown & null; // null
type T00 = unknown & undefined; // undefined
type T00 = unknown & null & undefined; // nerver
type T00 = unknown & string; // string
type T00 = unknown & string[]; // string[]
type T00 = unknown & any; // any
type T00 = unknown & unknown; // unknown

3.The relationship between unknown types and other types

The code is as follows (example):

let value: unknown;
let value1: unknown = value; //正确
let value2: any = value; // 正确
let value3: boolean = value; // 错误
let value4: string = value; // 错误
let value5: number = value; // 错误
let value6: object = value; // 错误
let value7: any[] = value; // 错误
let value8: Function = value; // 错误

4. Classic example of unknown type

type T50<T> = {
    
    [P in keyof T]: number};
type T51 = T50<any>; // {[key: string]: number}
type T52 = T50<unknown>; // {}

3. Application of unknown – combined with type contraction

declare function isFunction(x: unknown): x is Function;
function f20(x: unknown) {
    
    
	if (x instanceof Error) {
    
    x; // Error};
	if (isFunction(x)) {
    
    x;// Function}
}

Summarize

You can assign any type to a variable of the any type and perform any operation on the variable (no need to pay attention to the type, equivalent to js); you can also assign any value to a variable of the unkonw type, but you must perform type checking or assertion before the variable can be Operate variables (you must pay attention to the type of the variable).

Guess you like

Origin blog.csdn.net/qq_48896417/article/details/125075974