The official document: www.typescriptlang.org/docs/.

JavaScript is a generic language that cannot support large projects; Typescript is a strongly typed language that can support large project development and maintenance.

TypeScript is built by adding static type definitions on top of JavaScript and can be compiled into JavaScript code for execution. It features a powerful type system and support for the ES6 specification. TypeScript is hosted on GitHub.

Features:

  • Type system: type annotation, compile-time type checking, type inference, and type erasure
  • interface
  • The enumeration
  • Mixin
  • Generic programming
  • The namespace

Ported from the ECMA 6 specification:

  • class
  • The module
  • Arrow syntax for lambda functions
  • Optional and default parameters
  • Tuples [are actually arrays in JS, but typescript provides a different way of playing them than python’s progenitors]
  • await / async

TypeScript is a superset of Javascript that extends the syntax of Javascript and allows existing Javascript code to run in TypeScript unchanged. TypeScript code, meanwhile, can be converted into pure JavaScript code by TypeScript’s compiler, and the compiled JavaScript code can run in any browser. TypeScript compilation tools can also run on any server and on any system. TypeScript files have a.ts suffix.

The code to compile

main.ts

function main(person) {
    return "Hello, " + person;
}

var user = "Jane User";

document.body.textContent = main(user);
Copy the code

Terminal: TSC main.ts

Compiled code: main.js

function main(person) {
    return "Hello, " + person;
}
var user = "Jane User";
document.body.textContent = main(user);

Copy the code

The data type

The data type The keyword describe
Any type any If no type is declared, variables declared as any by default can be assigned values of any type.
Numeric types number The JavaScript equivalent of the number type, in TypeScript all numbers are floating point numbers and are of the number type.

let num1: number = 0b1010; / / binary

let num2: number = 0o744; / / octal

let num3: number = 6; / / decimal

let num4: number = 0xf00d; // Hexadecimal

** Note: **TypeScript and JavaScript do not have integers.
String type string A series of characters, using single quotes () or double quotation marks () to represent the string type. Backquotes to define multi-line text and inline expressions.

let name: string = “xiaoming”;

let qq_number: string= ‘50000000’;

Let text: string = ‘hello, my name is ${qq_number}’, my QQ number is ${qq_number} ‘;
Boolean type boolean There are only two values: true and false.

let sex: boolean = true;
An array type There is no Declare variables as arrays.

let arr: number[] = [1, 2]; // Add [] to the element type

let arr: Array<number> = [1, 2]; // Array generics
tuples There is no A tuple type is used to represent an array with a known number and type of elements. The types of each element need not be the same. Meta-ancestors are themselves supported in native JS.

let x: [string, number];

x = [‘xiaoming’, 16]; // It is running properly

x = [16, ‘xiaoming’]; / / an error

console.log(x[0]); / / output xiaoming
The enumeration enum Enumeration types are used to define collections of values.

enum Color {Red, Green, Blue};

let c: Color = Color.Blue;

console.log(c); 2 / / output
void void Used to identify the type of value returned by a method, indicating that the method has no return value.

function hello(): void {

alert(“Hello xiaoming”);

}
null null Empty.
undefined undefined undefined
never never Never is a subtype of other types, including null and undefined, and represents a value that never occurs.

New type: Union type: var Variable = Type 1 | Type 2 | Type 3…. ;

variable

Declaration of variables

  1. Method 1

Specify data type

varVariable name: type = value;Copy the code
  1. Method 2

No data type is specified

varVariable name = value;// var Variable name :any = value;
Copy the code
  1. Methods 3

Default variable and specify type

varVariable name: type;Copy the code
  1. methods

Specify the type and value. The default type is any and the default value is undefined

varThe variable name.Copy the code

If the data type is defined. An error occurs when a variable is updated if the type requirements are not met

var username:string = "hello world";
username = "xiaoming";
username = 123;  // error TS2322: Type 'number' is not assignable to type 'string'.
console.log(username);
Copy the code

Scope of a variable

  • Global scope

Defined outside the program structure and can be used anywhere in the code

  • The class scope

Also known as a property or field, a header position in a class that can be called not only from a class method, but also from a class method. The variable can be accessed by the class name.

Class variables can also be static variables, which can be accessed directly by the class name.

  • Local scope

Can only be used in a code block that declares her