00018-easy-tuple-length

Back

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

2.解答

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

3.元组和数组的区别与使用

数组只能一种类型,长度可变 元组可声明多种类型,长度固定

length 属性类型

数组number 类型(动态)

type Arr = string[];
type ArrLength = Arr['length']; // number - 普通数字类型

元组:具体的数字字面量类型(固定)

type Tuple = [string, number, boolean];
type TupleLength = Tuple['length']; // 3 - 具体的数字字面量类型

4.声明式编程(ts)和命令式编程(c)

命令式(C 语言):

声明式(TypeScript 类型系统):

你的代码问题分析

你写的:

type Length<T> = {
  i = 0                    // ❌ 类型系统中不能声明变量
  [P in T] = i++          // ❌ 不能使用循环和自增操作
}

所以我们应该使用TS类型思维:直接描述元组的长度就是它的length属性

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

readonly作用

1. 匹配 as const 断言的类型

const tesla = ['tesla', 'model 3', 'model X', 'model Y'] as const;
// 类型推断为:readonly ["tesla", "model 3", "model X", "model Y"]

2.TypeScript 的类型系统中,可变类型不能赋值给只读类型,但只读类型可以赋值给可变类型

因此,为了接受 as const 创建的只读元组,约束必须是 readonly any[]

Solution by nyk233233 #37464

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


In plain words

Length<T> constructs a new type by:

  1. Taking a readonly string array T.
  2. Looking up its length property.
  3. Producing the exact number of elements in the array as a type (a literal type, not just 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'];

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"];

心得与知识点

  1. 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 Rocco10086 #36537

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