type

There are many types of TypeScript, but I will not explain them one by one here. There are several types listed below, some you may not have heard, some you may not have used, some you may have used but you don’t know the meaning, and some you may know the meaning but cannot be distinguished from other types...

Symbol

ES6 introduces a new primitive data type Symbol, which represents a unique value. It is the seventh data type of the JavaScript language.

Using the Symbol() function, we can declare a Symbol variable. Note that the new command cannot be used because it is a primitive data type; the Symbol function can also accept a string as a parameter, which is mainly for convenience when the Symbol is converted to a string, it is easier to distinguish. The parameters passed in are supported ES2019 through the instance attribute description

Tuple

As we all know, objects, arrays, and enumeration types can store multiple data; however, the data stored in objects and enumerations exists in the form of key/value, and does not have special effects such as sorting. The array can only store the same type of data; Tuple type It can be regarded as a combination of the characteristics of the object type and the array type:

  • The data is in order;
  • Store different types of data;

There are no tuples in JavaScript. Tuples are a unique type in TypeScript, and their behavior is similar to arrays. Tuples can be used to define the type of unnamed attributes of limited number of Each attribute has an associated type. When using tuples, the value of each attribute must be provided, and the element can be accessed by index:

let tupleType: [string, boolean, number];
tupleType = ["hello", true, 2];

console.log(tupleType[0]); // hello
console.log(tupleType[1]); // true
console.log(tupleType[2]); //2

any (any type)

In TypeScript, any type can be classified as any type. This makes the any type the top type of the type system (also known as the global super type).

The any type is essentially a refuge in the type system, which gives developers a lot of freedom: TypeScript allows us to perform any operation on the value of the any type, including assignment and assignment, without any prior execution. Formal inspection. ␐Such as:

let value: any;
// 被赋值
value = 'hello world';
value = true;
value = [];
value = {};
value = 0;
// 赋值给其他变量
let temp: number;
temp = value;
// 操作属性/方法
value.toString();
value.length;
value();
  • any can be any type, so any attribute or method on the default variable can be found in a type (basic type/custom type) corresponding to it, so there is no problem in checking;

unknown (do not know what type)

Just as all types can be assigned to any, all types can also be assigned to unknown. This makes unknown another top-level type of the TypeScript type system (the other is any);

All assignments to the value variable are considered to be of the correct type. However, there are unexpected restrictions when trying to assign unknown values to other types of variables:

let value: unknown;
// 被赋值
value = 'hello world';
value = true;
value = [];
value = {};
value = 0;
// 赋值给其他变量
let temp: number;  // Error:Type 'unknown' is not assignable to type 'number'.
temp = value;

let temp2: any;
temp2 = value; // Success

let temp3: unknown;
temp3 = value; // Success

let temp4: string[]; // Error:Type 'unknown' is not assignable to type 'string[]'
temp4 = value;

// 操作属性/方法
value.toString(); // Error:Object is of type 'unknown'
value.length; // Error:Object is of type 'unknown'
value(); // Error:Object is of type 'unknown'
  • The unknown type can only be assigned to the any type and the unknown type itself: a container that can hold values of any type can hold values of the unknown type.
  • The value variable type is unknown, and the variable type cannot be confirmed when the check is performed. Therefore, the method or attribute on the variable is considered to be uncertain, and the check is not passed.

void (no type)

Void can be understood as the opposite of any, which means that there is no type. Void is generally used when a function does not use a return value. When a variable is defined as void type, it has no effect. The value of the variable can only be undefined

let value:void;
value = 0; // Error:Type 'number' is not assignable to type 'void'.
value = undefined; // Success

null (empty value type) and undefined (undefined type)

TypeScript ⾥, undefined and null have their own type values, undefined and null respectively, and can only be undefined and null:

let value:null;
value = undefined;  // Error:Type 'undefined' is not assignable to type 'null'
value = null; //Success
value = 1; // Error:Type '1' is not assignable to type 'null'

let value:undefined;
value = undefined; //Success
value = null; // Error:Type 'null' is not assignable to type 'undefined'
value = 1; // Error:Type '1' is not assignable to type 'undefined'

Object, object, and {} (object type)

# How to use Object, object and {} in TypeScript?

Never

never type indicates absence value type. The never type is used for:

  • Throw an exception
  • There will be no return value of the function expression or arrow function expression return value type ( no end, continue to execute !).
function error(msg: string)msgever {
         throw new Error(msg);
}
function loop(): never {
         while (true) {
                 // 一直执行下去
         }
}

Use never to avoid a new type of union that has no corresponding implementation. The goal is to write code that is absolutely type-safe; you can use the feature of the never type to check the completeness of the variable type:

// 定义type a为string或者number类型
type a = string | number;
// 方法checkWithNever用来检测参数是否是type a,
function checkWithNever(foo: a) {
    if (typeof foo === "string") {
        // 这⾥执行 string 类型的操作
    } else if (typeof foo === "number") {
        // 这⾥执行 number 类型的操作
    } else {
        // 在这⾥是 never用来接收非type a的类型,并且会在编译时报错
        const check: never = foo;
    }
}
// 重写 type a类型,但是没有修改checkWithNever,导致boolean 类型,⽆法赋值给 never 类型,执行后编译报错
type a = string | number | boolean;
const c: a = true;
checkWithNever(c); 
// Type 'boolean' is not assignable to type 'never'. 
// 'check' is declared but its value is never read.

哇喔WEB
156 声望1.3k 粉丝

欢迎交流学习