00018-easy-tuple-length

Back

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

type Length<T extends readonly string[] > = T["length"]
type Length<T extends readonly PropertyKey[] > = T["length"]
type Length<T extends readonly unknown[] > = T["length"]
type Length<T extends readonly unknown[] > = T extends { length: infer L } ? L : never 
// infer L means that L is a alias of length

Solution by binhdv155127 #34325

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

Solution by wxh-cyber #34293

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

Solution by souzaramon #34271

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

type Length2<T extends readonly any[], N = T['length']> = 
  T extends [infer F, ...infer R] 
    ? Length<R>
    : N;

Solution by bkdragon0228 #34252

// ไฝ ็š„็ญ”ๆกˆ

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

Solution by W-fitTiger #34245

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

Solution by Fmanuel809 #34190

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

Solution by ProvorovOleksii #34077

// 1์ฐจ ์‹คํŒจ โ€”> ๋ฐฐ์—ด์ด ์•„๋‹Œ ํƒ€์ž…์— ๋Œ€ํ•ด์„œ๋„  T['lengthโ€™];๋ฅผ ์ฐธ์กฐํ•˜๋ ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Œ
type Length<T> = T['lengthโ€™];

// ์„ฑ๊ณต (readonly๋ฅผ ๊ผญ ๋ถ™์—ฌ์•ผ ํ•˜๋‚˜ ์‹ถ์€๋ฐ ์•ˆ ๋ถ™์ด๋ฉด ์—๋Ÿฌ๋‚จ)
type Length<T extends readonly any[]> = T['length']

Solution by leeeunji93 #34034

// ์—ฌ๊ธฐ ํ’€์ด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”
type Length<T extends readonly any[]> = T['length'];

Solution by LeeKangHyun #34024

Restrict that input type should become extends readonly any[], and return length field.

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

Solution by dev-jaemin #33980

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

Solution by ouzexi #33966

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

Solution by iam1maker #33927

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

Solution by veralex #33747

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

Solution by laplace1009 #33700