【TS】联合类型

TS 联合类型

联合类型即通过管道 | 将变量设置多种类型,赋值时可以根据设置的类型来赋值。

1 普通变量声明

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

2 数组声明使用

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

3 函数中使用

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

猜你喜欢

转载自blog.csdn.net/qq_53931766/article/details/125331739