6. 获取元组长度
简单
创建一个通用的工具类型 Length<T>
,其中 T
是一个被 readonly
修饰的元组类型,返回这个元组类型的长度。
例如:
const tuple = ['tesla', 'byd', 'benz'] as const
const tupleNumber = [1, 2, 3, 4] as const
type TupleLength = Length<typeof tuple> // expected 3
type TupleNumberLength = Length<typeof tupleNumber> // expected 4
传入的类型必须是元组类型。