This is the 22nd day of my participation in Gwen Challenge

What is typescript?

  • Typescript is a super version of javascript
  • It must run through the responding compiler

The advantages of the typescript

  • There are better error reporting mechanisms
  • Better editor tips
  • Better semantic declaration and readability

Typescript runtime environment

Install typescript -g with TSC command

tsc demo.ts
Copy the code

2. The ts-Node installation can run directly in the Node state

ts-node demo.ts
Copy the code

An in-depth understanding of static typing and a description of various types

(1) When a variable type is applied, methods of the variable type are loaded

Const a:number = 2 // Then a can use the a*3 attribute methodCopy the code

(2) Basic types

number
string
boolean
null
undefined
symbol
void
Copy the code

(3) Object type

  • Class types
Class aa {};
let bb: aa;
Copy the code
  • functional
Const aa:()=>number
Copy the code
  • An array of
  • The date type

Type annotations and type inference

Type annotation Specifies the type of a variable assigned after it is declared

Type inference defines the type of a variable based on the value you assign to it

The return type of the ** function is best defined (return value type: void and never in addition to normal returns).

The way functions are declared

let func = (name:string):string => {
  return name + '111';
}
let func2:(name:string) => string = (name:string) => {
  return name + '111';
}
Copy the code

Arrays and tuples

(1) Array classes are similar to Javascript arrays

// Type alias type User = {name: string, age: number}; const UserArray: User[] = [];Copy the code

The tuple determines the subscript type of the array according to the order of the array

Let UserInfo: [string,number,string] = [' myuserinfo ', 18, 'myuserinfo '];Copy the code

Interface Interface

Unlike a type alias, a type alias can directly represent a data type, but an interface must be an object.

type UserName = string;
interface Users{
    name: string
}
Copy the code

(2) Attributes of the interface type can be defined as read-only

interface Users{
    readonly name: string
}
Copy the code

(3) Properties of the interface type can only indicate that they are known, and others can be replaced by propName

Interface Users {name: string, [propName:string] : any}Copy the code

(4) The Class implements interface, then the declared Class must include the attributes of the interface

type User = {name: string, age:number}
class UserClass implements User {
    name: 'perter.wang';
    age: 18;
}
Copy the code

(5) Interface inheritance

type User = {name: string, age:number}
interface Users extends User {
    sex: string
}
Copy the code

The concept of class

(1) Create and use classes

Class User {name: string = 'mudan'; getName() { return this.name; }; setUserName(name: string) { this.name = name; Let user = new user (); User.setusername (' luoyang white tree '); user.getName();Copy the code

(2) Class inheritance and rewriting

Class User {name: string = 'mudan'; getName() { return this.name; }; setUserName(name:string){ this.name = name; } } class UserMater extends User { boss:string = 'wangtiantian' getBoss() { return this.boss; }; Return super.getName() + this.getboss (); return super.getName() + this.getboss (); Let company = new UserMater(); company.getBossAndstaff();Copy the code

The method and access type corresponding to the attributes of the class

(1) Private,protected,public access type

  • The default access type is public, which allows calls and assignments to be made inside or outside a class

  • Private is only allowed within classes

  • Protected is allowed within classes and in inherited subclasses

(2)

  • Constructor is automatically executed when the class is instantiated
class ZaerDa { name:string = 'link'; arms:string; constructor(name:string,arms:string){ this.name = name; this.arms = arms; this.link(); } link() {console.log(' Connecting... '); Console. log(' name: '+ this.name); Console. log(' weapon '+ this.arms); } } let gamer = new ZaerDa('wpfly','MasterSword');Copy the code
  • When a subclass inherits its parent class and creates its own constructor, it calls the constructor of the following parent class manually, passing in the parameters required by the parent constructor
class Links extends ZaerDa { constructor() { super('wpfly','MasterSword'); }}Copy the code

Getters and setters in a class

(1) Getter

  • When a private variable in a class is inaccessible, you can define a getter to get the value of that property
class ZaerDa { private name:string; private arms:string; constructor(name:string,arms:string){ this.name = name; this.arms = arms; } get getName() { return this.name; // Call this method directly from outside}}Copy the code

(2) setters

  • When a private variable in a class cannot be assigned a value, you can define a setter to get the value of that property
class ZaerDa { private name:string; private arms:string; constructor(name:string,arms:string){ this.name = name; this.arms = arms; } set setName(name:string){ this.name = name; }}Copy the code

(3) Comprehensive use of singleton mode

class ZaerDa { private palyer:ZaerDa; Private constructor(){} static getPlay(){if(! this.palyer){ this.palyer = new ZaerDa(); } return this.palyer; }}Copy the code

An abstract class

(1) Definition of abstract classes • Many classes that have a common property or method define an abstract class to integrate those methods together • Abstract classes can only be inherited and cannot be instantiated (2) Definition and use of abstract classes

abstract class Palyers{ abstract palyer(): number; }; class PalyerOne extends Palyers{ palyer(){ return 1; }}Copy the code