// 类型断言
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
}))
浙公网安备 33010602011771号