Yuzhe's Blog

yuzhes

3057 · Push

Posted at # TypeScript # TypeChallenge # TC-Easy
EN ·

3057 · Push

Challenge Link

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:

  1. T extends any[] — constrains T to be an array/tuple type
  2. [...T, U] — spreads all elements of T and appends U at the end

This mirrors what Array.prototype.push does at runtime, but at the type level.

Key concept: