Boolean, number, string, any, NULL, undefined, and object

// Only need to declare the variable followed by the type description; Note: A variable declaration must be followed by an assignment, otherwise it is left undefined by default

var bool:boolean = true;
var num:number = 123;
var str:string = '123';
let an:any = 12;
let num5:null = null;
let num:undefined ;
let num3:undefined = undefined;
let num3:object = {a:1};
Copy the code

Special case: a special declaration

let num1:undefined ;
let num2:number | undefined;
let num2:boolean | number | string | any | null | undefined;
Copy the code

voidtype

The method name followed by the data type represents the return value type of the function method.

Note: void indicates no return value;

function run() :void{
	console.log(123)};function main() :number{
	return 123;
}
Copy the code

Never type

The type never is a subtype of any other type (including all other types); It’s not going to happen;

Case 1: A function that returns never must have an unreachable endpoint

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

Case 2: Never reach case (see youyuxi example)

The branching condition in the last else in this example is never possible, and any type other than never can be assigned a type of never;

So let’s say I make a change in the code in the future that changes the type of A. For example, if you change it to a Boolean type, and you assign it to false you’ll get to the last branch and you’ll get an error;

var a: number = Math.random();
if(a>0.5) {}else if(a<0.5) {}else if(a==0.5) {}else {var b: never = a }
Copy the code

An array type

An array type requires a restriction on each item in the array, which is one more restriction than the base type above;

Declaration method 1: Use a modified type with Angle brackets

var arr:Array<number> = [1.2.3]; Inside Angle brackets is the type of the contents of the arrayvar arr0:Array<any> = ['str'.2.true];
Copy the code

Declaration method 2: Use a modified type with brackets (brackets denote arrays)

var arr1:number[] = [1.2.3];      
var arr2:any[] = ['str'.1.true];
Copy the code

The type of the tuple is tuple

A tuple is a type of array; It’s more demanding. It has to be one to one

var arr:[number,boolean] = [1.true]; There has to be a one-to-one correspondenceCopy the code

Enumeration type declaration mode: enum after the variable name is defined.

Note: If no value is declared; The default is the subscript, the default is the increment of 1, the default is the undefined, the default is the string;

enum Err{
	undefined=1.null."str" = 2             
}
var e:Err = Err.null;
console.log(e)/ / 2
var e1:Err = Err.undefined;
console.log(e1)/ / 1
Copy the code

Element numbering should avoid conflicts

Because we can number the enumeration name corresponding to the number backward

var name: string = Err[1];
console.log(name); 
Copy the code

// Display ‘undefined’ because its value is 1 in the above code