TypeScript
TypeScript是JavaScript类型的超集,它可以编译成纯JavaScript。
TypeScript可以在任何浏览器、任何计算机和任何操作系统上运行,并且是开源的。
安装
1 2 3
| npm install -g typescript
|
接口声明
1 2 3 4 5 6
| export default interface Sociality { makeFriend():Boolean doGame():void }
|
类的声明并导出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| import Sociality from './Sociality';
export default class Person implements Sociality { private name:string private age:number private sex: number constructor(name:string, age:number, sex:number) { this.name = name; this.age = age; this.sex = sex; } public getName():string { return this.name; } public getAge():number { return this.age; } public getSex():number { return this.sex; } makeFriend():boolean { return true; } doGame():void { console.log('我们在做游戏'); } }
|
类的继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import Person from './Person';
export default class Girl extends Person { private high:number constructor(name:string, age: number) { super(name, age, 1); }
public setHigh(high: number):void { this.high = high; }
public getHigh():number { return this.high; } }
|
编译生成.d.ts文件
编译生成js文件