TypeScript
约 150 个字 36 行代码 预计阅读时间 1 分钟
简介
TypeScript是微软基于JS构建的一种强类型的语言,强制执行严格的类型检查,所以TS包含JS
具体而言就是在定义变量的时候可以给定一个类型,就像是C语言的int
,比如
增加的语法
接口
相当于C++中的抽象类,比如
interface User{
_id: string;
name: string;
is_admin?: boolean;// ?表示可选项
}
const user: User={
_id: "123",
name: "shihao"
}
interface Shape{
color: "blue" | "green" | "red";
}
interface Circle extends Shape{
radius: number;
}
字面类型
允许指定一个变量只能取特定的字面量值