接口/类型别名的扩展(继承)
- 接口(interface)使用extends关键字
interface Animal {
name: string
}
interface Bear extends Animal{
honey: boolean
}
const bear: Bear = {
name: "winie",
honey: true
}
console.log(bear.name);
console.log(bear.honey);
type Animal = {
name: string
}
type Bear = Animal & {
honey: boolean
}
const bear: Bear = {
name: "winie",
honey: true
}
向现有的类型添加字段
- 接口
接口可以重复声明,每次声明的字段都会追加到相同接口名中
interface My {
title: string
}
interface My {
count: number
}
const s: My = {
title: "123",
count: 12
}
- 类型别名
类型别名不可以重复声明,也不能向已有的类型别名中添加新字段