Advanced TypeScript Exercises - Question 6

Pragmatic Maciej - Mar 4 '20 - - Dev Community

Hi folks! ✋ Today we will play a bit with mapped types. The exercise is about getting all value types from the tuple type. Question will be slightly different (as I am still playing with the series format) and will be split into two tasks with different difficulty level.

6.1 Naive version (lower difficulty)

type NaiveFlat<T extends any[]> = unknown // 🔥 here your code
// test case
type Naive = [['a'], ['b', 'c'], ['d']];
type NaiveResult = NaiveFlat<[['a'], ['b', 'c'], ['d']]>
// should evaluate to "a" | "b" | "c" | "d"
Enter fullscreen mode Exit fullscreen mode

Our goal is to make type level function NaiveFlat which will take nested tuple and get all value types from it. Naive implementation should work with 1 level of nesting so tuple of tuples.

6.2 Deep version (higher difficulty)

type DeepFlat<T extends any[]> = unknown // 🔥 here your code
// test case
type Deep = [['a'], ['b', 'c'], [['d']], [[[['e']]]]];
type DeepTestResult = DeepFlat<Deep>  
// should evaluate to "a" | "b" | "c" | "d" | "e"
Enter fullscreen mode Exit fullscreen mode

We go level up. Deep version should flat any level of nested tuples.
The questions and ready to start code is available in The Playground

Post your answers in comments. Have fun! Answer will be published soon!

BTW please share your opinion if such questions with few options works for you? I want to know what you think about such format, thanks!

This series will continue. If you want to know about new exciting questions from advanced TypeScript please follow me on dev.to and twitter.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player