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