首页 > 其他分享 >二. TS语法细节

二. TS语法细节

时间:2022-12-08 21:35:25浏览次数:32  
标签:console log TS number 语法 细节 printId id string

1. 联合类型

function printId(id: string | number){
  console.log(id);
}
printId(123)
printId("abc")
export {}

通常需要使用类型缩小(做一些判断后再使用)

function printId(id: string | number){
  if(typeof id === 'string'){
    console.log(id.toUpperCase());
  }else {
    console.log(id);
  }
}
printId("abc")
export {}

标签:console,log,TS,number,语法,细节,printId,id,string
From: https://www.cnblogs.com/yufenchi/p/16967388.html

相关文章