// 1. 使用 type 关键字来创建类型别名。// 2. 类型别名(比如此处的Point),可以是任意合法的变量名称。typePoint={
x:number;
y:number;};// 3. 创建类型别名后,直接使用类型别名作为变量的类型注释即可。 functionprintCoord(pt: Point){console.log("The coordinate's x value is "+ pt.x);console.log("The coordinate's y value is "+ pt.y);}printCoord({ x:100, y:100});
可以使用类型别名为任何类型命名,而不仅仅是对象类型,常用于联合类型。
typetest=number;//基本类型let num: test =10;typeuserOjb={name:string}// 对象typegetName=()=>string// 函数typedata=[number,string]// 元组typenumOrFun=number| getName // 联合类型