1.1. 联合类型
联合类型通常与 null 或 undefined 一起使用:
const hello = (name: string|undefined)=>{
console.log("hello "+name)
}
hello("wanmait")
hello(undefined)这里 name 的类型是 string | undefined 意味着可以将 string 或 undefined 的值传递给hello 函数。
1.2. 交叉类型
交叉类型是将多个类型合并为一个类型,把多种类型叠加到一起成为一种类型,它包含了所需的所有类型的特性。
interface Person {
name: string
age: number
}
interface Student {
school: string
}
const student: Person & Student = {
name: 'xiaoma',
age: 15,
school: 'Wanmait',
}
console.log(student.name)1.3. 类型别名
type newType = string|number;
let s:newType;
s = "hello";
s=100;
type PersonStudent = Person & Student
const student2:PersonStudent={
name: 'xiaoma',
age: 15,
school: 'Wanmait',
}

0条评论
点击登录参与评论