00018-easy-tuple-length

Back

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

Solution by pofigrist #35852

// 你的答案
type Length<T extends readonly any[]> = T['length']

Solution by song4613107 #35787

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

Solution by Sensuele #35760

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

Solution by XenoPOMP #35740

// 你的答案
type Length<T extends readonly string[]> = T['length']

Solution by ndwgg #35543

// 你的答案
type Length<T extends readonly string[]> = T['length']

Solution by ndwgg #35542

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

Solution by thukyaw11 #35528

// 여기 풀이를 입력하세요
type Length<T> = T extends {length: number} ? T["length"] : never

Solution by Gwanghun-Im #35499

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

Solution by gangnamssal #35469

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

or

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

Solution by RanungPark #35432

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

Solution by Sathiyapramod #35418

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

Solution by Sathiyapramod #35412

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

Solution by Pig-Cola #35366

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

Solution by gyeounjeong #35354

type Length<T extends readonly any[]> =
  T extends [infer first, ...infer rest] ? Length<rest> : 1

I was so excited because this's my first time coming up with a solution before referring others :)

Solution by watanaki #35187

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

Solution by raeyoung-kim #34937

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

Solution by Kim-Ji-Seop #34924

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

Solution by jsk3342 #34905

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

Solution by 56aiden90 #34876

문제 설명

배열(튜플)을 받아 길이를 반환하는 제네릭 Length<T>를 구현하세요.

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

풀이

읽기전용 unknown 타입의 배열로 타입을 제한하고 T["length"] 인덱스 접근을 통해 타입의 전체 길이를 반환하여 문제를 해결하였다.

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

Solution by MyeonghoonNam #34831

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

Solution by eunsukimme #34810

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

Solution by kang-kibong #34744

Completely forgot that if we use const, then type is inferred with maximum accuracy, which allows to know specific details such as exact length of touple :)) without const, Length will just return number type

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

Solution by Git-I985 #34699

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

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

Solution by lephuthuc2001 #34698

// 你的答案
type Length<T extends readonly any[]> = T['length']

Solution by yunxiaopw #34683

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

Solution by zeyuanHong0 #34675

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

Solution by nathan2slime #34657

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

Solution by devshinthant #34542

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

Solution by ktim816 #34421

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

Solution by rookie-luochao #34358