type

TypeScript has many types, but I won’t cover them all here. Here’s a list of some you may not have heard of, some you may not have used, some you may have used but don’t know what they mean, and some you may know what they mean but can’t distinguish them from others

Symbol

ES6 introduces a new primitive data type, Symbol, representing unique values, which is the seventh data type of the JavaScript language.

Using the Symbol() function we can declare a Symbol variable. Note that we cannot use the new command because it is the original data type. The Symbol function can also take a string as an argument, mainly to make it easier to distinguish between symbols when converted to a string. This passed argument is supported in ES2019 through the description instance attribute.

Tuple (Tuple)

As we all know, objects, arrays, enumerated types can hold multiple data; However, objects and enumerations store data in the form of key/value and do not have sorting effects. Arrays can only store data of the same type. The Tuple type can be thought of as a combination of object and array characteristics:

  • Data order;
  • Store different types of data;

There are no tuples in JavaScript. Tuples are TypeScript specific types that work like arrays. Tuples can be used to define the types of a limited number of unnamed attributes. Each attribute has an associated type. When using tuples, the value for each attribute must be provided, and elements 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
Copy the code

Any (any type)

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

The Any type is essentially a refuge from the type system, which gives developers a lot of freedom: TypeScript allows us to do anything with any values, including assigning and being assigned, without performing any kind of checking beforehand. Such as:

let value: any;
/ / be assignment
value = 'hello world';
value = true;
value = [];
value = {};
value = 0;
// Assign to other variables
let temp: number;
temp = value;
// Manipulate properties/methods
value.toString();
value.length;
value();
Copy the code
  • Any can be of any type, so any property or method on the default variable can find a type (base type/custom type), so there is no problem checking;

Unknown (unknown type)

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

All assignments to the value variable are considered typed correctly. However, there are unexpected limitations when trying to assign a value of type unknown to a variable of other types:

let value: unknown;
/ / be assignment
value = 'hello world';
value = true;
value = [];
value = {};
value = 0;
// Assign to other variables
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;

// Manipulate properties/methods
value.toString(); // Error:Object is of type 'unknown'
value.length; // Error:Object is of type 'unknown'
value(); // Error:Object is of type 'unknown'
Copy the code
  • An unknown type can only be assigned to any and the unknown type itself: a container that can hold a value of any type can hold a value of the unknown type.

  • Value The type of the variable is unknown. During the check, the type of the variable cannot be confirmed. Therefore, methods or attributes on the variable are considered to be uncertain and the check fails.

Void (no type)

Void can be interpreted as the opposite of any, meaning that there is no type. Void is used when a function does not return a value. Void has no effect on a variable; the value can only be undefined

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

Null (null type) and undefined (undefined type)

In TypeScript, undefined and null have values of their own type 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'
Copy the code

Object, Object, and {} (Object types)

How do we use Object, Object, and {} in TypeScript?

Never

The never type represents the type of a value that does not exist. The never type is used for:

  • An exception is thrown
  • A function expression that does not return a value or the return value type of an arrow function expression (no end, go on and on!). .
function error(msg: string)msgever {
 		throw new Error(msg);
}
function loop() :never {
 		while (true) {
 				// All the way through}}Copy the code

Use never to avoid the occurrence of new union types that have no corresponding implementation. The purpose is to write type-safe code. We can use the never feature to fully check variable types:

// Define type A as string or number
type a = string | number;
// The checkWithNever method is used to check whether the parameter is type A,
function checkWithNever(foo: a) {
    if (typeof foo === "string") {
        // Perform string operations here
    } else if (typeof foo === "number") {
        // Perform an operation of type number here
    } else {
        // In this case, never is used to receive types that are not type A and will report errors on compilation
        const check: never= foo; }}// Overwrite type A, but do not change checkWithNever, Boolean, cannot be assigned to never, compiler error
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.
Copy the code