This is the 18th day of my participation in the August Genwen Challenge.More challenges in August

preface

TypeScript is a superset of JavaScript types that can be edited to pure JavaScript. TypeScript runs on any browser, any computer, and any operating system, and is open source.

Basic types of

Tuples

Tuple allows the element types of arrays to be different, such as defining a set of tuples with Boolean and String values

let x: [boolean.string];
x = [true.'10'];
x = [5.'10']; // Type 'number' is not assignable to type 'boolean'.
Copy the code

When accessing an out-of-bounds (non-existent) element, Union Types are used instead

x[2] = true; // OK
x[5] = 9; // Error
Copy the code

The joint type Said the value can be one of many types, using | separated for each type, such as: string | number

Enumeration (Enum)

Enum is a complement to the JavaScript standard data type, and is used to define collections of values, such as small to medium size, January, February, and March

enum Size {Large, Middle, Small};
enum Date {Jan, Feb, Mar};
let s: Size = Size.Large;
let d: Date = Date.Feb;
Copy the code

Element numbering starts at 0 by default and can be specified or assigned at all

enum Size {Large = 1, Middle = 2, Small = 4};
let s: Size = Size.Large;
Copy the code

The convenience of an enumeration type is that you can go from the value of the enumeration to the corresponding name

enum Size {Large = 1, Middle = 2, Small = 4};
let s: string = Size[4];

console.log(s); // Small
Copy the code

Any

Any can be assigned to Any type of value

let a: any = 1;
a = '1';
a = true;
Copy the code

Never

Never represents the types of values that Never exist. For example, the never type is the return type of function expressions or arrow function expressions that always throw an exception or have no return value at all

function infiniteLoop() :never {
    while (true) {}}Copy the code

Object

Object represents a non-primitive type, that is, a type other than number, string, Boolean, symbol, NULL, or undefined.

At this point, all data types have been described

Welcome to follow my official account: Class A Coder and get daily dry goods push. Thank you again for reading. I’m Han the Programming Monkey