1. 非空断言操作符(!)
function test(name: string | undefined | null) {
const onlyStringName: string = name
const ignoreUndefinedAndNullName: string = name!
}
type CallbackString = () => string
function test(call: CallbackString | null | undefined) {
const onlyStringName = call()
const ignoreUndefinedAndNullName = call!()
}
2. 可选链操作符(?.)
const test = obj?.other?.dir
3. 空值合并运算符(??)与 逻辑或运算符( || )
const file = null ?? 'dir'
const num = 0 ?? 20
const num1 = 0 || 20
4. 可选属性运算符(?:)
interface Student {
name: string
age: number
gender?: number
}
5. 运算符(&)
type Pointx = { x: number }
type Ponity = { y: number }
type Point = Pointx & Ponity
let point: Point = { x: 1, y: 1 }
6. 运算符(|)
const fun = (info: string | null | undefined) => {}
7. 数字分隔符(_
)
const number1 = 1234_5678
const number1 = 12345678