typeScript学习-TS类型-null和undefined
typeScript学习
null 和 undefined
undefined any unknown 可以接受 undefined
let data: undefined = undefined let data2: any = undefined let data3: unknown = undefined
null any unknown 可以接受 null
let data4: null = null let data5: any = null let data6: unknown = null
let str: string | undefined = undefined str = "abc" console.log("str:", str) // 可选参数? function fn(data?: string) { if (data) data.toString() } function fn2(data: string | undefined) { } // 默认参数 function fn3(data: string = "abc") { } fn() // 参数可传可不传 // fn2() // 报错,参数必传 fn2(undefined) fn3()