元组长度
huihuilang53 opened this issue · comments
/*
18 - 获取元组长度
by sinoon (@sinoon) #简单 #tuple
题目
欢迎 PR 改进翻译质量。
创建一个通用的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
在 Github 上查看:https://tsch.js.org/18/zh-CN
*/
/* _____________ 你的代码 _____________ */
type Length<T extends readonly any[]> = 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, 4>>,
Expect<Equal<Length, 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
*/