-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00018-easy-tuple-length.ts
53 lines (41 loc) · 1.26 KB
/
00018-easy-tuple-length.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
18 - Length of Tuple
-------
by sinoon (@sinoon) #easy #tuple
### Question
For given a tuple, you need create a generic `Length`, pick the length of the tuple
For example
```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
```
> View on GitHub: https://tsch.js.org/18
*/
/* _____________ Your Code Here _____________ */
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">
];
/* _____________ Further Steps _____________ */
/*
> Share your solutions: https://tsch.js.org/18/answer
> View solutions: https://tsch.js.org/18/solutions
> More Challenges: https://tsch.js.org
*/