Remove the last element from an array. Learn array type operations in this medium-level challenge on TypeScriptPro.
In this medium-level challenge, you'll implement a generic Pop<T> that removes the last element from an array.
Implement a generic Pop<T> that takes an Array T and returns an Array without it's last element.
For example
type arr1 = ['a', 'b', 'c', 'd']
type arr2 = [3, 2, 1]
type re1 = Pop<arr1> // expected to be ['a', 'b', 'c']
type re2 = Pop<arr2> // expected to be [3, 2]Extra*: Similarly, can you implement Shift, Push and Unshift as well?
Change the following code to make the test cases pass (no type check errors).
type Pop<T extends unknown[]> = T extends [...infer Keep, infer Last] ? Keep : THow it works:
T to be an array with extends unknown[]T extends [...infer Keep, infer Last] matches the array type and collects all elements except the last one into Keep? Keep returns the array type with the last element removedT if the array is emptyThis challenge is originally from here.
Be the first to access the course, unlock exclusive launch bonuses, and get special early-bird pricing before anyone else.
Only 27 Spots left
Get 1 month early access
Pre-Launch discount