interface Person { firstName: string, lastName: string } function greeter(person: Person) { return person.firstName + " " + person.lastName; } var user = {firstName: 'Jane', lastName: 'User'}; console.log(greeter(user));
class Student { fullName: string; // 构造函数参数中使用public是一种简写形式,它将自动创建具有该名称的属性 constructor(public firstName, public middleInitial, public lastName) { this.fullName = firstName + " " + middleInitial + " " + lastName; } } interface Person { firstName: string, lastName: string } function greeter(person:Person) { return person.firstName + " " + person.lastName; } var student = new Student('Jane', 'M', 'User'); console.log(greeter(student));
生成的js代码:
chrome调试ts文件: