interface
interface 与 type
type
能够表示非对象类型,而interface
只能表示对象类型(包括数组、函数等)。interface
可以继承其他类型,type
不支持继承。this
关键字只能用于interface
。type
可以扩展原始数据类型,interface
不行。interface
无法表达某些复杂类型(比如交叉类型和联合类型),但是type
可以。
interface
是对象的模板,可以看作是一种类型约定,中文译为“接口”。使用了某个模板的对象,就拥有了指定的类型结构。实现该接口很简单,只要指定它作为对象的类型即可。
typescript
// 定义接口
interface Person {
firstName: string
lastName: string
age?: number
gender?: boolean
}
// 实现接口
const zhangSan: Person = {
firstName: '三',
lastName: '张'
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
对象方法
对象的方法共有三种写法。
typescript
interface A {
f(x: boolean): string
}
interface B {
f: (x: boolean) => string
}
interface C {
f: { (x: boolean): string }
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
继承
interface
可以使用 extends
关键字,继承其他 interface
。
继承 interface
typescript
interface A {
firstName: string
}
interface B extends A {
lastName: string
}
const zhangSan: B = {
firstName: '张',
lastName: '三'
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
继承 type
警告
如果 type
不是 对象类型
则无法继承
typescript
type Name = {
firstName: string
lastName: string
}
interface A extends Name {
age?: number
}
const zhangSan: A = {
firstName: '张',
lastName: '三'
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
继承 class
interface
还可以继承 class
,即继承该类的所有成员。
typescript
class Person {
firstName: string = '张'
getFirstName() {
return this.firstName
}
}
interface Z extends Person {
lastName: string
}
const zhangSan: Z = {
firstName: '张',
lastName: '三',
getFirstName(): string {
return this.firstName
}
}
console.log(zhangSan.getFirstName()) // 张
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
多重继承
typescript
interface A {
firstName: string
}
interface B {
lastName: string
}
interface C extends A, B {
age?: number
}
const zhangSan: C = {
firstName: '张',
lastName: '三'
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
接口合并
如果两个接口名称一致,则会自动合并接口
typescript
interface A {
firstName: string
}
interface A {
lastName: string
}
const zhangSan: A = {
firstName: 'zhang',
lastName: 'san'
}
console.log(zhangSan.lastName, zhangSan.firstName)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14