【TS】joint type

TS union type

联合类型That is, the variable |is , and the value can be assigned according to the set type when assigning a value.

1 Ordinary variable declaration

let res: number|string|boolean
res = 12
res = '小徐'
res= true
//只能赋值指定的类型,如果赋值其它类型就会报错。

2 Array declarations use

let arr: number[]|string[]
arr = [1,2]
arr = ['xiao','xu']

3 functions used in

const test = (p: string | number) => {
    
     ... }

Guess you like

Origin blog.csdn.net/qq_53931766/article/details/125331739
Recommended