【基础语法】:
【Option Chain】:
- 有值的话就取 , 没有值的话就不取~~~!
type Person = {
name: string // #必选
friend?: {
name: string // #必选(如果有朋友)
age?: number,
girlFriend?: {
name: string // #必选(得先有朋友) -> (如果有女朋友)
}
}
}
const info: Person = {
name: "why",
friend: {
name: "kobe",
girlFriend: {
name: "lily"
}
}
}
//假如下面的代码是在另外一个文件当中~~~!
console.log(info.name)
// console.log(info.friend!.name)
console.log(info.friend?.name)
console.log(info.friend?.age)
console.log(info.friend?.girlFriend?.name)
export{}
【参数的可选类型】:
- 可选类型必须放在必选类型的后面~~~!