**
TypeScript基础篇(总结官网)
**
1.安装
npm install -g typescript
2.编译代码
在命名行 运行编译器 :tsc greeter.ts
function greeter(person:string){ // 给person函数的参数添加 :string 类型注解
return person;
}
3.类型注解
// 类型不可混乱
4.接口
// 使用接口来描述一个拥有firstName和lastName字段的对象
interface Person {
firstName: string;
lastName: string;
}
function greeter(person: Person) {
return "Hello, " + person.firstName + " " + person.lastName;
}
let user = { firstName: “Jane”, lastName: “User” };
document.body.innerHTML = greeter(user);
5.类
// 创建一个Student类,它带有一个构造函数和一些公共字段
class Student {
fullName: string;
constructor(public firstName, public middleInitial, public lastName) {
this.fullName = firstName + " " + middleInitial + " " + lastName;
}
}
interface Person {
firstName: string;
lastName: string;
}
function greeter(person : Person) {
return "Hello, " + person.firstName + " " + person.lastName;
}