3057 · Push
Less than 1 minuteTC-Easy
3057 · Push
Problem
Implement the generic version of Array.push.
type Result = Push<[1, 2], '3'> // [1, 2, '3']Solution
type Push<T extends any[], U> = [...T, U]Explanation
We use tuple spreading to construct a new tuple that appends U to the end of T.
Step by step:
T extends any[]— constrainsTto be an array/tuple type[...T, U]— spreads all elements ofTand appendsUat the end
This mirrors what Array.prototype.push does at runtime, but at the type level.
Key concept:
- Variadic Tuple Types —
[...T, U]creates a new tuple type withUappended
