元组

// 元组
const arr:[string, boolean, number] = ['123', false, 123]

arr.push(456)
// console.log(arr[3]) // 元组越界之后,虽然可以push,但是实际上访问不到,arr[3]报错
// arr.push({1:'1'}) // 会推断arr的类型是[string | boolean | number],传进去其他类型会报错

// 获取元组的类型
type first = typeof arr[0]
// 获取元组的类型长度
type length = typeof arr['length']

 

posted on 2025-02-14 12:04  ChoZ  阅读(5)  评论(0)    收藏  举报

导航