type Length<T extends readonly any[]> = T["length"]
Here, T["length"] is the length of the type(array). readonly
is used for the const
array like tesla or spaceX. Everything else is self-explanatory!
Solution by Anonymous961 #36963
解题思路
元组有一个方法T['length']
可以获取T
对象的length
属性值,在这里可以直接获取到T
的数组长度
题解
type Length<T extends readonly any[]> = T["length"];
心得与知识点
T['length']
可以获取T
元组的长度Solution by lkwavestian #36947
// your answers
type Length<T extends readonly any[]> = T['length']
Solution by AlexBraunMagic #36917
type Length<T extends readonly PropertyKey[]> = T['length']
Solution by sarvar-hub #36897
type Length<T> = T extends {length: infer L} ? L : never;
Solution by shaishab316 #36842
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'>,
]
Solution by AnastasiaSv #36762
type Length<T extends readonly unknown[]> = T['length']
Solution by Abdullah-Elsayed01 #36753
type Length<T extends readonly string[]> = T['length']
Solution by tungulin #36714
type Length<T extends string[]> = T["length"]
Solution by Mamdouhreda #36708
// 你的答案
type Length<T extends readonly any[]> = T['length']
Solution by PurplePlanen #36700
type Length<T> = T extends readonly [...infer A] ? T['length'] : never
Solution by LaFocus #36688
type Length<T extends readonly any[]> = T['length']
Solution by seungdeok #36660
type Length<T extends readonly any[]> = T['length'];
Solution by tjd985 #36615
// 你的答案
type Length<T extends readonly any[]> = T['length'] extends 0 ? 0 : T['length']
// type Length<T extends readonly any[]> = T['length']
Solution by MrSissel #36581
// 你的答案
type Length<T extends readonly any[]> = T['length']
Solution by mola-fish #36576
type Length<T extends readonly any[]> = T['length']
Solution by ChemieAi #36550
type Length<T extends readonly any[]> = T['length']
Solution by UsGitHu611 #36495
type Length<T extends {
length: number;
}> = T['length']
Solution by rinkeshpurohit #36461
type Length<T extends readonly any[]> = T['length']
Solution by gakki-san #36443
type Length<T extends readonly any[]> = T['length']
Solution by alirezaprime #36412
// your answers
type Length<T extends readonly any[]> = T['length'];
Solution by justBadProgrammer #36359
// 你的答案
type Length<T extends readonly any[]> = T['length']
Solution by ATravelerGo #36352
type Length<T extends readonly unknown[]> = T["length"]
Solution by asylbekduldiev #36331
type Length<T extends readonly any[]> = T['length']
Solution by 1Alex4949031 #36310
type Length<T> = T extends readonly any[] ? T['length'] : never
Solution by Jace254 #36267
type Length<T extends Readonly<Array<any>> > = T['length']
Solution by Maxim-Do #36225
type Length<T extends readonly any[] > = T['length']
Solution by EvgeniyKoch #36194
type Length<T extends readonly string[]> = T['length']
Solution by tungulin #36155
type Length<T extends readonly string[]> = T['length']
Solution by AleksandrShcherbackov #36142