?:表示属性或参数为可选项interface Person {
name: string,
age: number,
weight?: number
}
const person = new Person(name='Bob', age=18)
??:空值合并运算符。当左侧操作数为 null 或 undefined 时,其返回右侧的操作数,否则返回左侧的操作数。console(person.weight ?? 0)
!:表示类型推断排除 null、undefinedfunction myFunc(maybeString: string | undefined | null) {
const onlyString: string = maybeString;
const ignoreUndefinedAndNull: string = maybeString!;
}
!!:用于将类型转换为布尔值。其中 null、undefined、0、'' 转换后为 false,其他都为 true。let a = null
console.log(!!a)
let b = 'hello world'
console.log(!!b)
let bob = {name: 'Bob', age: 18}
console.log(!!bob)