## 1.题目
创建一个`Length`泛型,这个泛型接受一个只读的元组,返回这个元组的长度。
例如:
```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
type Length<T extends readonly any[]> = T['length']
数组只能一种类型,长度可变 元组可声明多种类型,长度固定
数组:number
类型(动态)
type Arr = string[];
type ArrLength = Arr['length']; // number - 普通数字类型
元组:具体的数字字面量类型(固定)
type Tuple = [string, number, boolean];
type TupleLength = Tuple['length']; // 3 - 具体的数字字面量类型
你写的:
type Length<T> = {
i = 0 // ❌ 类型系统中不能声明变量
[P in T] = i++ // ❌ 不能使用循环和自增操作
}
所以我们应该使用TS类型思维:直接描述元组的长度就是它的length属性
type Length<T extends readonly any[]> = T['length']
as const
断言的类型const tesla = ['tesla', 'model 3', 'model X', 'model Y'] as const;
// 类型推断为:readonly ["tesla", "model 3", "model X", "model Y"]
因此,为了接受 as const
创建的只读元组,约束必须是 readonly any[]
。
Solution by nyk233233 #37464
type Length<T extends readonly string[]> = T["length"]
<T extends readonly string[]>
T
.T
must be a readonly array of strings (like ["a", "b", "c"] as const
).T["length"]
length
property of the array type T
.T
is a tuple (because of the readonly
), the length
will be a literal number type (e.g., 3
) instead of just number
.Length<T>
constructs a new type by:
T
.length
property.number
).Solution by 3aluw #37423
type Length<T extends readonly any[]> = T["length"]
Solution by Nakamura25257 #37257
type Length<T extends readonly unknown[]> = T['length'];
readonly unknown[]
Solution by djdidi #37120
type Length<T extends readonly any[]> = T['length']
Solution by 359Steve #37014
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