前言
该博文是我在学习typeScript时作的一笔记。 官方网站
正文
安装
npm install -g typescript
特性
- 类型注解
- TypeScript里的类型注解是一种轻量级的为函数或变量添加约束的方式。
function greeter(person: string) {
return \"Hello, \" + person;
}
let user = \"Jane User\";
document.body.innerHTML = greeter(user);
- 接口
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);
- 类
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;
}
let user = new Student(\"Jane\", \"M.\", \"User\");
document.body.innerHTML = greeter(user);