Back to Type Challenges

README

questions/34286-hard-take-elements/README.md

latest1.7 KB
Original Source
<!--info-header-start--><h1>Take Elements </h1><blockquote><p>by Eirik Måseidvåg <a href="https://github.com/Eirmas" target="_blank">@Eirmas</a></p></blockquote><p><a href="https://tsch.js.org/34286/play" target="_blank"></a> </p><!--info-header-end-->

Implement a type Take<N, Arr> that returns the first N elements from an array Arr. If N is negative, return the last |N| elements

For example,

ts
type T0 = Take<2, [1, 2, 3]> // [1, 2]
type T1 = Take<3, ['1', 2, true, false]> // ['1', 2, true]
type T2 = Take<-2, [1, 2, 3]> // [2, 3]
type T3 = Take<0, [1, 2, 3]> // []
type T4 = Take<5, [1, 2, 3]> // [1, 2, 3]
type T5 = Take<3, []> // []
<!--info-footer-start-->

<a href="../../README.md" target="_blank"></a> <a href="https://tsch.js.org/34286/answer" target="_blank"></a> <a href="https://tsch.js.org/34286/solutions" target="_blank"></a> <hr><h3>Related Challenges</h3><a href="https://github.com/type-challenges/type-challenges/blob/main/questions/00216-extreme-slice/README.md" target="_blank"></a> <!--info-footer-end-->