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"
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"
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.
Top comments (6)
This time, after a lot of thought & attempts :P
Naive version -
Deep version-
Playground link
Nice but the solution can be simpler 😉
Yeah, realised it later. Updated the answer :)
Deep Version -- Hard way:
Deep version -- easy way:
Cool exercise! Like this format!
Tnx Nikita for your feedback. Maybe will get back to the series some day. Had lack of motivation lately. Tnx again