Skip to main content

Push

Implement the generic version of Array.push

For example:

ts
type Result = Push<[1, 2], '3'> // [1, 2, '3']
Cannot find name 'Push'.2304Cannot find name 'Push'.
ts
type Result = Push<[1, 2], '3'> // [1, 2, '3']
Cannot find name 'Push'.2304Cannot find name 'Push'.
Solution ✅
ts
type Push<Arr, Value> = Arr extends readonly any[] ? [...Arr, Value] : never;
 
type Result = Push<[1, 2], '3'> // [1, 2, '3']
type Result2 = Push<[], 1> // [1]
type Result3 = Push<[1, 2], '3'> // [1, 2, '3']
type Result4 = Push<['1', 2, '3'], boolean> // ['1', 2, '3', boolean]
ts
type Push<Arr, Value> = Arr extends readonly any[] ? [...Arr, Value] : never;
 
type Result = Push<[1, 2], '3'> // [1, 2, '3']
type Result2 = Push<[], 1> // [1]
type Result3 = Push<[1, 2], '3'> // [1, 2, '3']
type Result4 = Push<['1', 2, '3'], boolean> // ['1', 2, '3', boolean]