Advanced TypeScript Exercises - Question 9
Pragmatic Maciej
Posted on April 25, 2020
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.
đź’– đź’Ş đź™… đźš©
Pragmatic Maciej
Posted on April 25, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
javascript How I Created Vanilla Calendar Pro — A Lightweight and Flexible JavaScript Calendar with TypeScript
November 28, 2024