【TypeScript】五、类型断言|联合类型|交叉类型
// 类型断言
interface A {
    name: string
}
interface B {
    age: string
}
let f1 = function(type: A|B): void {
    let x = (<A>type).name
    let y = (type as A).name
}
// 联合类型
let phone: number | string = '18520210193'
let fn2 = function(type: number | boolean):boolean {
    return !!type
}
console.log(fn2(1))
console.log(fn2(true))
// 交叉类型
interface A {
    name: string
}
interface B {
    age: number
}
let fn = function(type: A & B):boolean {
    return !!type
}
console.log(fn({
    name: "zhangsan",
    age: 14
}))
posted on 2022-07-05 17:07  仓鼠不爱吃辣条  阅读(39)  评论(0)    收藏  举报

页尾

页尾

页尾

页尾

页尾

页尾

页尾