【??】?? 和 || 相似,不同的是0对于 ?? 来说为真,对 || 来说为假
console.log(0 || 6) // 6
console.log(0 ?? 6) // 0
【?:】可选参数,相当于加上了一个undefined
export interface dataType {
id: number;
name: string;
age: number;
hobby?: string; // 相当于 string | undefined
}
【?.】?. 相当于 &&
const food = {
name:'apple',
color:'red'
}
// food?.name 相当于 food && food.name
console.log(food?.name); // 'apple'
console.log(food && food.name); 'apple'
【!.】!. 断言,告诉TS该对象一定存在某值
在一些情况下,TS会警告我们某值可能为空,但我们知道调用的时候一定有值,这时就可以使用!.来告诉TS一定有值
option!.children