type Push<T extends unknown[], U> = [...T, U]
<T extends unknown[], U>
T
must be an array (tuple) of any types.U
can be any single type (the element to push).Concat
, this doesn't use readonly
, so it's slightly more flexible but works the same way.[...T, U]
T
using the spread ...T
U
as the last elementU
appended to the end.Push<T, U>
constructs a new tuple type by:
T
.U
at the end.type A = [1, 2, 3]
type B = ["hello", "world"]
type Test1 = Push<A, 4> // [1, 2, 3, 4] ✅
type Test2 = Push<B, "!"> // ["hello", "world", "!"] ✅
type Test3 = Push<[], "first"> // ["first"] ✅
type Test4 = Push<[1], [2, 3]> // [1, [2, 3]] ✅ (pushes the whole array as one element)
Solution by 3aluw #37492
type Push<T extends any[], U> = [...T, U]
Solution by Nakamura25257 #37262
type Push<T extends unknown[], U> = [...T, U];
Solution by djdidi #37137
type Push<T extends unknown[], U> = [...T, U]
Solution by Anonymous961 #37046
type Push<T extends any[], U> = [...T, U]
Solution by 359Steve #37020
// that's cool, thanks all Ts developer
type Push<T extends any[] , U> = [...T, U]
Solution by leongaooo #36928
// your answers
type Push<T extends readonly any[], U> = [...T, U];
Solution by AlexBraunMagic #36912
type Push<T extends unknown[], U> = [...T, U];
Solution by shaishab316 #36848
type Push<T extends unknown[], U> = [...T, U]
Solution by Abdullah-Elsayed01 #36791
type Push<T extends any[], U> = [...T, U];
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<Push<[], 1>, [1]>>,
Expect<Equal<Push<[1, 2], '3'>, [1, 2, '3']>>,
Expect<Equal<Push<['1', 2, '3'], boolean>, ['1', 2, '3', boolean]>>,
]
type errors = [
// @ts-expect-error
Expect<Equal<Push<number[], string>, string[]>>,
// @ts-expect-error
Expect<Equal<Push<string[], number>, [string, number]>>,
]
Solution by AnastasiaSv #36768
type Push<T extends any[], U> = [...T, U]
Solution by tungulin #36719
// 여기 풀이를 입력하세요
type Push<T extends readonly any[], U> = [...T, U];
Solution by seungdeok #36682
// your answers
type Push<T, U> = T extends [...infer TType] ? [...TType,U] : never
Modifications and Suggestions are welcomed 🎉
Solution by SubramaniyanTN #36675
type Push<T extends readonly any[], U> = [...T, U]
This implementation is better for the following reasons: Greater Compatibility: Supports both readonly and regular array types, making it suitable for more scenarios.
Avoids type errors caused by readonly input types.
Aligns with TypeScript Best Practices: Using readonly any[] is more general, reducing potential type conflicts.
It will be better than type Push<T extends any[], U> = [...T, U]
Solution by spike014 #36625
type Push<T extends any[], U> = [...T, U]
Solution by tungulin #36608
type Push<T extends any[], U> = [...T, U]
Solution by ChemieAi #36556
type push<T extends Array<unknow>, U> = [...T, U]
Solution by Divcutu #36455
type Push<T extends readonly any[], U> = [...T, U];
Solution by alirezaprime #36417
// your answers
type Push<T extends readonly any[], U> = [...T, U];
Solution by justBadProgrammer #36392
type Push<T extends readonly any[], U> = [...T , U]
Solution by HelloWook #36384
type Push<T extends any[], U> = [...T,U]
Solution by tac-tac-go #36361
type Push<T extends any[], U> = [...T, U]
Solution by 1Alex4949031 #36316
type Push<T extends unknown[], U> = [...T,U]
Solution by asylbekduldiev #36291
type Push<T extends any[], U> = [...T, U]
Solution by AleksandrShcherbackov #36147
type Push<T extends any[], U> = [...T, U]
Solution by reonce #36050
type Push<T extends any[], U> = [...T, U]
Solution by karazyabko #36032
type Push<T extends unknown[], U> =[...T, U]
Solution by codingaring #35942
type Push<T extends unknown[], P> = [...T, P]
Solution by Sensuele #35802
// 你的答案
type Push<T extends readonly unknown[], U extends unknown> = [...T, U]
Solution by naruto-823 #35795
type Push<T extends any[], U> = [...T, U]
Solution by ydkim120 #35732