loading...
Cover image for Advanced TypeScript Exercises - Question 9

Advanced TypeScript Exercises - Question 9

macsikora profile image Maciej Sikora ・1 min read

Advanced TypeScript Exercises (20 Part Series)

1) Advanced TypeScript Exercises - Question 1 2) Advanced TypeScript Exercises - Answer 1 3 ... 18 3) Advanced TypeScript Exercises - Question 2 4) Advanced TypeScript Exercises - Answer 2 5) Advanced TypeScript Exercises - Question 3 6) Advanced TypeScript Exercises - Answer 3 7) Advanced TypeScript Exercises - Question 4 8) Advanced TypeScript Exercises - Answer 4 9) Advanced TypeScript Exercises - Question 5 10) Advanced TypeScript Exercises - Answer 5 11) Advanced TypeScript Exercises - Question 6 12) Advanced TypeScript Exercises - Answer 6 13) Advanced TypeScript Exercises - Question 7 14) Advanced TypeScript Exercises - Answer 7 15) TypeScript Exercises Bonus🦠 - Type of Pandemia 16) TypeScript Exercises Bonus🦠 - Answers Part 1 17) TypeScript Exercises Bonus🦠 - Answers Part 2 18) Advanced TypeScript Exercises - Question 8 19) Advanced TypeScript Exercises - Answer 8 20) Advanced TypeScript Exercises - Question 9

Short but very interesting question ahead of you. Write a type which will represent not empty array NonEmptyArray. Type should behave in the same way as original Array is behaving, with this difference that empty array is a value which is not a valid member of NonEmptyArray

type NonEmptyArray<T> = /* your type level code here 💪 */
const a: NonEmptyArray<string> = [] // should be compilation error 🛑
const b: NonEmptyArray<string> = ['Hi TS'] // should be ok! 👌

Watch out, there is not one possible solution. How many solutions can you make? Post your answers in comments (preferred links to the playground). Have fun! Answer will be published soon!

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

Advanced TypeScript Exercises (20 Part Series)

1) Advanced TypeScript Exercises - Question 1 2) Advanced TypeScript Exercises - Answer 1 3 ... 18 3) Advanced TypeScript Exercises - Question 2 4) Advanced TypeScript Exercises - Answer 2 5) Advanced TypeScript Exercises - Question 3 6) Advanced TypeScript Exercises - Answer 3 7) Advanced TypeScript Exercises - Question 4 8) Advanced TypeScript Exercises - Answer 4 9) Advanced TypeScript Exercises - Question 5 10) Advanced TypeScript Exercises - Answer 5 11) Advanced TypeScript Exercises - Question 6 12) Advanced TypeScript Exercises - Answer 6 13) Advanced TypeScript Exercises - Question 7 14) Advanced TypeScript Exercises - Answer 7 15) TypeScript Exercises Bonus🦠 - Type of Pandemia 16) TypeScript Exercises Bonus🦠 - Answers Part 1 17) TypeScript Exercises Bonus🦠 - Answers Part 2 18) Advanced TypeScript Exercises - Question 8 19) Advanced TypeScript Exercises - Answer 8 20) Advanced TypeScript Exercises - Question 9

Posted on Apr 25 by:

macsikora profile

Maciej Sikora

@macsikora

I am Software Developer, currently interested in static type languages (TypeScript, Elm, Reason) mostly in the frontend land

Discussion

markdown guide
 

My first solution :D

type NEA<T> = [T, ...T[]]
 
[deleted]
 

So why doesn't this work for an array of length 2?

 

TypeScript has endless magic, doesn’t stop to amaze me.

 

@macsikora I actually added a NonEmptyArray type to my codebase a while ago. It’s been useful, but not as much as I had hopped.

Have you found this type useful in a real world setting?

From memory the main “problem” was things like ‘map’ don’t “flow” the underlying type through...