The installation

Global installation

npm install typescript -g

After the installation is complete, configure the environment and check whether the installation is successful

tsc -v

tsc --version

Check whether the installation is successful

Create a project

tsc –init

Installation types/nodes package

npm install @types/node –dev-save

Run a TS compiled JS file

node hello.js

Definition of a function

function search(age: number, name: string, pre: string = 'pre', stature? : string): string { console.log(stature) return '' + age + name; } var age: number = 18; var name1: string = "xiaobai" var search1 = search(age, name1); console.log(search1) function searchArgs(... args: any[]): string { let result: string = ''; for (let i = 0; i < args.length; i++) { result += args[i]; } return result; } console.log(searchArgs(1, 2, 'white ', 3, 5, true, false, null))Copy the code

The function definitions

Function add(n1:number,n2:number):number {return n1+n2; } console.log(add(1,2)) / / var add1 = function (n1:number,n2:number) {return n1+n2; } console.log(add1(1,2)) // arrow function var add2 = (n1:number,n2:number):number=>{return n1+n2; } the console. The log (add2 (1, 2))Copy the code

An array of

let jspang = { name:"name", website:"baidu.com", age:18, say:function () { console.log('front') } } console.log(jspang.name); Jspang.say () // array array // let arr1:number[]; // let arr2:Array<string>; let arr1:number[] = []; Let arr2: number [] =,2,3,5,6 [1]; let arr3:Array<string> = ["1","1","1","1",]; let arr4:Array<boolean> = [true,false,false,false]; Let ARR5 :Array<string> = new Array("1","1","1","1", "1","1",) let ARR6 :Array< Boolean > = new Array(true,false,false,false) // ancestor let x :[string,number] x = ['hello',111]Copy the code

class

class Demo11 {
​
    name: string;
    age: number;
​
    constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    }
​
    say() {
​
        console.log(this.name)
        console.log(this.age)
​
    }
​
​
}
​
​
let demo11 = new Demo11("xiaobai",20);
console.log(demo11);
demo11.say()
​
Copy the code

Object-oriented – Class modifier

class Demo12 {
    public sex:string;
    protected name:string;
    protected age:number;
​
    public constructor(name:string,age:number,sex:string) {
        this.sex = sex;
        this.name = name;
        this.age = age;
    }
    public say(){
        console.log("hello")
    }
    protected love(){
        console.log("love")
    }
​
}
​
​
​
var demo12 = new Demo12("xiaobai",20,"girl");
​
demo12.say()
Copy the code
Class Demo12 {// Public readOnly sex:string = "girl"; } var demo12 = new Demo12(); / / / / demo12. Sex () errorCopy the code

Class inheritance and rewriting

​
class Demo13 {
​
​
    public name:string;
    public skill:string;
    public age:number;
    constructor(name:string,skill:string,age:number) {
        this.name = name;
        this.skill = skill;
        this.age = age;
    }
​
    inter(){
        console.log('inter')
    }
​
}
​
// var demo13 = new Demo13("name","skill",20);
​
class Child  extends Demo13{
​
    public text:string ="goods";
​
    inter(){
        super.inter()
        console.log('child-inter')
    }
    public goods(){
        console.log("goods")
    }
​
}
​
var child = new Child("name","skill",20);
child.inter()
child.goods()
​
Copy the code

interface

interface Interf { sex:string; inter:string; maiBB? :Boolean; // Optional} let interf: interf = {sex:"girl", inter:"look book", maiBB:true,} console.log(interf)Copy the code
interface Demo14 { (source:string,subString:string):boolean; } let demo:Demo14; demo =function (source:string,subString:string):boolean{ let flag = source.search(subString) return (flag! = 1); } console.log(demo(" ah,sfdaf,asfa"," ah ",));Copy the code

The namespace

namespace Goods{
    export class Nice {
​
        public name:string ="xiaobai";
        say(){
            console.log("say")
        }
    }
​
}
namespace Goods1{
    export class Nice {
​
        public name:string ="xiaobai-Goods1";
        say(){
            console.log("Goods1-say")
        }
    }
​
}
​
​
var nice = new Goods.Nice();
console.log(nice)
​
var nice1 = new Goods1.Nice();
console.log(nice1)
​
Copy the code


\