类型注解

1
2
3
4
5
6
7
8
9
// js

function greeter(person) {
}

// ts

function greeter(person: string) {
}

接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
interface Person{
firstName: string,
lastName: string
}

function greeter(person: Person) {
}

let user = {
firstName: 'J',
lastName: 'N'
};
// 内部结构兼容则兼容
greeter(user);

1
2
3
4
5
6
7
8
9
10

// 使用public可以自动创建该名称的属性
class Student {
fullName: String,
constructor(public firstName: string, public lastName: string) {
this.fullName = firstName+lastName;
}
}
let user = new Student('J', 'N');