TS environment installation and test installation

① : Install Node. js and NPM

Use node -v and NPM -v to check whether the installation is successful. If the version number is displayed, the installation is successful.

② : Install TS

Command: NPM install -g typescript

Check the installation: Run the TSC -v command to display the version number, indicating that the installation is successful

Ii.TS basic use and vscode automatic compilation Settings

1: Basic use

① : Create the 1.ts file

let aa : String='Niu Zhikun'; alert(aa);Copy the code

② : Run the TS file on the terminal command line

Command: TSC file path

After execution, the corresponding JS file is generated

2: enable vscode to compile automatically

① : Use TSC –init command in the root directory to generate tsconfig.json file

② : Cancel the outDir comment in the JSON file and set the path name of the saved folder

③ : select the top terminal of vscode -> run tasks -> TSC monitor

Three: TS variable declaration

Variable declaration format in TS:

Let/const /var Variable name: Data type = variable value Short let variable name = variable value

Joint type let variable names: data type 1 2 | | data type… = a variable’s value

The variable data type cannot be changed after it is specified and an error will be reported

String: let STR: string = ‘STR’/” STR “/ · STR ·;

Boolean: let bool: Boolean = true/false;

Note: Boolean values in TS cannot use 0/1 to represent true and false

Let num:number = 1;

null : let nul:null = null;

undefined : let nud:undefined =undefined;

Array: let arr: element data type [] = [element of specified data type,……]

Let arr: Array< element data type > = [element of specified data type,……]

Tuple: An array that specifies the data type and number of elements. Since arrays in TS cannot contain different types of elements, tuples can be used instead

Let arr: [string, number, number] = [‘ 123 ‘, 14, 32]

When defining tuple data, there must be a one-to-one correspondence according to the specified element data type and quantity

Access tuple data as an array

Enum: Enumeration can be understood as an object in JS. The declaration mode is the literal declaration of the object

Method 1: enum Enumeration name {Enumeration item = Enumeration value,……. } Enumeration entries are alphabetic and numeric. Enumeration values are integer numbers

Method 2: enum Enumeration name {Enumeration item,……. } Enumeration items default to generate the corresponding enumeration value, which is the subscript of the item, starting at 0

Any: indicates that the data type of the retrieved data cannot be determined. This is common in DOM fetching

let a: any =document.getElementById(‘id’);

Viod: Used to define methods that have no return value

function alerts(): void { alert(“Hello ts”); }

function getNum(): number{ return 1; }

Four functions

Function declaration

Function Function name (parameter 1: type,….) : Return value type {} The number of participating parameters must be of the same type

Function Name () : return value type {}

Function name (argument) Function name ()

Optional parameter function Function name (parameter 1? : types,…). : Return value type {}

Call with arguments: function name (argument) No arguments: function name ()

Function Function name (parameter 1: type = default,….) : Return value type {}

Function Function name (parameter 1: type,… Parameter 2: type []) : return value type {}

Five categories

The class name of the class {

/ / field

Variable name: type; .

// constructor

Constructor (Variable name: type,….) {

This. Variable name = variable name; .

}

// disp():void {

do something…..

}

}