typescript 可选参数

typescript 函数中的可选参数,参数默认值 必须在末尾

function bird(name: string, age: number = 0){

  console.log(age)

}

//替换age默认值

bird("jessi",5)

//使用age参数默认值

bird("jessi")

 

function bird(name: string, age?: number ){

  console.log(age)

}

//此时age作为可选参数,传入参数值

bird("jessi",5)

//此时age作为可选参数,可不传参

bird("jessi")

 

posted @ 2022-10-07 21:19  yurro  阅读(67)  评论(0)    收藏  举报