huihuilang53 / ts-challenges

ts类型体操刷题总结

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

元组长度

huihuilang53 opened this issue · comments

commented

/*
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
*/