18 - 获取元组长度
原创2025/4/28小于 1 分钟
18 - 获取元组长度
by sinoon (@sinoon) #简单 #tuple
题目
创建一个Length
泛型,这个泛型接受一个只读的元组,返回这个元组的长度。
例如:
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
在 Github 上查看:https://tsch.js.org/18/zh-CN
代码
/* _____________ 你的代码 _____________ */
type Length<T extends readonly PropertyKey[]> = T['length']
测试用例
/* _____________ 测试用例 _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
const tesla = ['tesla', 'model 3', 'model X', 'model Y'] as const
const spaceX = ['FALCON 9', 'FALCON HEAVY', 'DRAGON', 'STARSHIP', 'HUMAN SPACEFLIGHT'] as const
type cases = [
Expect<Equal<Length<typeof tesla>, 4>>,
Expect<Equal<Length<typeof spaceX>, 5>>,
// @ts-expect-error
Length<5>,
// @ts-expect-error
Length<'hello world'>,
]
相关链接
分享你的解答:https://tsch.js.org/18/answer/zh-CN
查看解答:https://tsch.js.org/18/solutions
更多题目:https://tsch.js.org/zh-CN