跳至主要內容

18. Tuple Length

鸭梨小于 1 分钟

18. Tuple Length

<题目描述>

::: playground#ts 18. Tuple Length

@file index.ts

// ============= Test Cases =============
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'>,
]

// ============= Your Code Here =============
type Length<T> = any

:::

点击查看答案
type Length<T extends readonly any[]> = T['length']