此页内容

✔️ Length of Tuple

pengzhanbo

167字小于1分钟

2022-12-01

题目

Github: length of tuple

创建一个通用的Length,接受一个readonly的数组,返回这个数组的长度。

type tesla = ['tesla', 'model 3', 'model X', 'model Y']
type spaceX = ['FALCON 9', 'FALCON HEAVY', 'DRAGON', 'STARSHIP', 'HUMAN SPACEFLIGHT']

type teslaLength = Length<tesla> // expected 4
type spaceXLength = Length<spaceX> // expected 5

解题思路

在 javascript 中, 通过 length 属性获取数组的长度,在类型上,也同样可以通过 length 获取数组的长度。 同时,需要约束 T 的类型为 只读数组。

答案

type Length<T extends readonly any[]> = T['length']

参考