类型别名:
顾名思义就说一个类型起了一个其他的名字:
如:
//类型别名
type name = string;
let zjq: name = '123'
给string类型起了一个名字叫name,后面定义string类型的变量,我就可以:name来实现:string的效果。
例子1:
//类型别名
type strName = string;
type numName = number;
type funReturn = (c1: strName, c2?: numName) => strName
let zjqStr: strName = '123';
let zjqNum: numName = 123
const myFun: funReturn = (c1: strName, c2: numName =666): strName => {
return c1 + c2.toString()
}
myFun(zjqStr, zjqNum)
myFun(zjqStr)
console.log(myFun(zjqStr, zjqNum), 'myFun(zjqStr, zjqNum)');
console.log(myFun(zjqStr), 'myFun(zjqStr)');
例子2:
//类型别名
type strName = string;
type numName = number;
type newName = strName | numName
let newZjq : newName
const myNewFun: (c1: newName) => newName = (c1: newName): newName=>{
return c1
}
newZjq =123
myNewFun(newZjq)
console.log("myNewFun(newZjq)", myNewFun(newZjq))
newZjq='123'
myNewFun(newZjq)
console.log("myNewFun(newZjq)", myNewFun(newZjq))