混合类型| 、交叉类型& 、断言as
// 联合类型 |, 代表可以传字符串或者数字 // const a = (name: string | number) => { // console.log(name); // } // a('gg') // a(18) // 交叉类型 &, 代表需要满足2个interface // interface a { // name: string // } // interface b { // age: number // } // const c = (arg: a & b) => { // console.log(arg); // } // c({name: 'gg', age: 11}) // 断言 as const a = (name: string | number):void => { // console.log(name.length); // 此处报错number类型不存在length属性,调用的方法应该是联合类型共有的方法才不会报错 // 第一种写法 console.log((name as string).length); // 第二种写法 console.log((<string>name).length); } a(123) // 实际上还是输出不了length