?:
是指可选参数,能够理解为参数自动加上undefinedcode
function echo(x: number, y?: number) { return x + (y || 0); } getval(1); // 1 getval(1, null); // error, 'null' is not assignable to 'number | undefined'
interface IProListForm { enterpriseId: string | number; pageNum: number; pageSize: number; keyword?: string; // 可选属性 }
?? 和 || 的意思有点类似,可是又有点区别,??相较||比较严谨, 当值等于0的时候||就把他给排除了,可是?? 不会orm
console.log(null || 5) //5 console.log(null ?? 5) //5 console.log(undefined || 5) //5 console.log(undefined ?? 5) //5 console.log(0 || 5) //5 console.log(0 ?? 5) //0
?.的意思基本和 && 是同样的a?.b 至关于 a && a.b ? a.b : undefined
get
const a = { b: { c: 7 } }; console.log(a?.b?.c); //7 console.log(a && a.b && a.b.c); //7