Split a Tuple into Head and Tail
Write the code, see it run, pass the checks.
The problem
- medium
- Code
Export types Head<T> and Tail<T> for a tuple type T: Head is its first element's type (undefined for an empty tuple) and Tail is a tuple of the rest ([] for an empty tuple). Then write head(tuple), which returns the first item, and tail(tuple), which returns a new array of the others, leaving the tuple unchanged. Type them with Head and Tail so that tail(['a', 1, true]) is [number, boolean], not (string | number | boolean)[].
Your code must pass
- head returns the first item
- tail returns the rest as a new array
- Has no type errors
- Head and Tail split a tuple type
- head and tail keep each position's type