手册
目录
类型别名允许使用自定义名称(别名)定义类型。
类型别名可用于像 string 这样的基本类型,或者像 object 和 array 这样更复杂的类型:
type CarYear = number
type CarType = string
type CarModel = string
type Car = {
year: CarYear,
type: CarType,
model: CarModel
}
const carYear: CarYear = 2001
const carType: CarType = "Toyota"
const carModel: CarModel = "Corolla"
const car: Car = {
year: carYear,
type: carType,
model: carModel
};
接口类似于类型别名,但只适用于 object 类型。
interface Rectangle {
height: number,
width: number
}
const rectangle: Rectangle = {
height: 20,
width: 10
};
接口可以扩展其他接口的定义。
扩展接口意味着您正在创建一个具有与原始接口相同属性的新接口,并添加了一些新内容。
interface Rectangle {
height: number,
width: number
}
interface ColoredRectangle extends Rectangle {
color: string
}
const coloredRectangle: ColoredRectangle = {
height: 20,
width: 10,
color: "red"
};
相关
视频
RELATED VIDEOS
科技资讯
1
2
3
4
5
6
7
精选课程
共5课时
17.2万人学习
共49课时
77万人学习
共29课时
61.7万人学习
共25课时
39.3万人学习
共43课时
70.9万人学习
共25课时
61.6万人学习
共22课时
23万人学习
共28课时
33.9万人学习
共89课时
125万人学习