C/C++教程

[Typescript Challenges] 5. Easy - Length of Tuple

本文主要是介绍[Typescript Challenges] 5. Easy - Length of Tuple,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

For given a tuple, you need create a generic Length, pick the length of the tuple

For example:

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

 

/* _____________ Your Code Here _____________ */

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


/* _____________ 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'>,
]

 

这篇关于[Typescript Challenges] 5. Easy - Length of Tuple的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!