首先下载typescript
npm i typescript
以下是greet.ts:
class User {
fullName: string
firstName: string
lastName: string
constructor(firstName:string, lastName:string) {
this.firstName = firstName
this.lastName = lastName
this.fullName = firstName+' '+lastName
}
}
interface Person {
firstName: string
lastName: string
}
let greeters = (person: Person) => {
return 'hello '+person.firstName+'--'+person.lastName;
}
let user1 = new User('yee','hu')
console.log(greeters(user1));
在命令行输入:
tsc greet.ts(编译生成greet.js)
node greet.js(启动greet.js文件)
输出结果:
hello yee--hu