00018-easy-tuple-length

Back

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

Solution by onoxm #38430

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

Solution by ssviaam #38379

type Length<T extends readonly PropertyKey[]> = T["length"]

Solution by M2S-SD #38359

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

Solution by VarunMendre #38337

// 你的答案

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

Solution by insvaia #38234

// your answers
type Length<T extends readonly any[]> = T extends {length: infer Length} ? Length : never

Solution by jospinchimi #38231

// your answers
type Length<T extends readonly any[]> = T['length'];

Solution by kyu-rong #38202

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

Solution by Tofu-Xx #38118

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

Get the length attribute from the generic T

Solution by mainakgupta33 #38068

type Length<T extends readonly unknown[]> = T["length"];

Solution by ahm4dd #38042

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

Solution by Nikita0x #38024

// your answers
type Length<T extends readonly any[]> = T["length"];

Solution by Maleeran #38018

// your answers
type Length<T extends any[]> = T['length'];

Solution by istamal #37893

type Length<T extends unknown[]> = T['length'];

Solution by ClarityOfMind #37876

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

Solution by tuannvnguyen #37872

 // answer :)

 type Length<T> = T extends { length: infer Length} ? Length : 0

Solution by guilhermeviiniidev #37861

/*
  18 - Length of Tuple
  -------
  by sinoon (@sinoon) #easy #tuple

  ### Question

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

  For example:

  ```ts
  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

View on GitHub: https://tsch.js.org/18 */

/* _____________ 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, 4>>, Expect<Equal<Length, 5>>, // @ts-expect-error Length<5>, // @ts-expect-error Length<'hello world'>, ]

Solution by SammyDotDev #37858

// your answers

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

Solution by xincheng-1999 #37841

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

Solution by Hansol46 #37832

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

Solution by pkutsenko #37822

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

Solution by jisooooooooooo #37788

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

Solution by kaeuhy #37763

// your answers

Solution by Brahima-Fofana #37759

type Length<T> = T extends readonly unknown[] ? T['length'] : never

Solution by rauf322 #37700

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

Solution by ortima #37687

type Length<T extends readonly any[]> = T extends { length: infer L } ? L : never

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<typeof tesla>, 4>>,
  Expect<Equal<Length<typeof spaceX>, 5>>,
  // @ts-expect-error
  Length<5>,
  // @ts-expect-error
  Length<'hello world'>,
]

Solution by PosionEdgar #37679

// your answers
type Length<T extends readonly unknown[]> = T["length"];

Solution by AlexanderNP #37648

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

Solution by BernTheSeal #37633

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

Solution by tomo-local #37576

// your answers

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

Solution by PAVANT009 #37560