?
可选选项
如:
interface User {
name: string;
age?: number
}
const u: User = { name: 'x' }
这里age为可选项,创建u这个变量不填age属性不会报错,这里的age类型就是 number | undefined
?.
可选链
const user: any = {
info: {
city: '北京'
}
}
const city = user?.info.city;
??
空值合并运算符(补齐|| 运算符, 只有左侧是null或undefined,才会返回右侧)
const user1: any = {
name: '李四',
index: 0
}
const name1 = user1.name ?? '暂无姓名';
const name2 = user1.name || '暂无姓名';
const index1 = user1.index ?? '暂无 index'; // 0
const index2 = user1.index || '暂无 index'; // 暂无 index
!
非空断言操作符
function fn1(a?: string) {
return a!.length; // ! 表示你已经知道了 a 不会是undefined
}
_
数字分隔符
const million = 1_000_000; // 1000000
&
交叉类型
见我的另一篇文章 TS中交叉类型和联合类型是什么
|
联合类型
见我的另一篇文章 TS中交叉类型和联合类型是什么
class私有属性
见我的另一篇文章#和private有什么区别?