TypeScript的外围准则之一是对值所具备的构造进行类型查看。
一、对象类型接口
TS应用接口来定义对象的类型。
接口(interface):是对象的状态(属性)和行为(办法)的形象(形容)。
实例:需要是创立一个Person的对象,须要对该对象进行肯定的束缚:
1、id是number类型,必须有,只读
2、name是string类型,必须有
3、age是number类型,必须有
4、gender是string类型,可没有
语法:应用interface关键字,接口名个别加上大写的i

interface 接口名{    属性名:值类型         //必须蕴含该属性    readonly 属性名:值类型 //该属性必有且只读    属性名?:值类型        //该属性可有可无}
//定义一个接口,蕴含以上规定interface IPerson{    readonly id: number //readonly-该属性为只读    name: string    age: number    gender?: string //?-该属性可有可无}//定义一个对象,该对象的类型是接口IPersonlet p1: IPerson = {    id: 1,    name: 'Liane',    age: 18,    gender: 'female'}

二、函数类型接口
将接口作为函数的类型,用来束缚函数的参数列表和返回值类型等。
语法:

interface 接口名 {    (形参名:值类型,形参名:值类型):返回值类型}
//定义一个函数类型的接口interface SearchFunc{    (source: string,subString: string):boolean}//定义一个函数,类型为SearchFunclet mySearch:SearchFunc = function(a:string,b:string):boolean{//形参名不必须跟接口统一    return a.search(b) > -1}console.log(mySearch('Liane','a'))

三、类类型接口
应用接口作为类的类型。
语法:

interface 接口名{    办法名() //该办法没有任何的实现}

应用时:

class 类名 implements 接口名{    /*类外面必须蕴含接口中的属性和办法*/}
//定义一个类的接口IFlyinterface IFly{    fly()}//定义一个类,类型为IFlyclass Bird implements IFly{    fly(){        return 'I can fly'    }}let b1 = new Bird()console.log(b1.fly()) 

一个类能够实现多个接口

//一个类实现多个接口//定义一个类的接口IEatinterface IEat{    eatApple()    eatRice()}//定义一个类的类型为IFly和IEatclass SuperMan implements IFly,IEat{    fly(){        return 'I can fly'    }    eatApple(){        return 'I can eat a apple'    }    eatRice(){        return 'Rice is delicious'    }}let superMan = new SuperMan()console.log(superMan.fly())console.log(superMan.eatApple())

接口能够继承
语法:
interface 接口名 extends 接口名1,接口名2,... {}

//定义一个接口,继承其余多个接口interface IMix extends IFly,IEat{}//类实现接口的形式同上