00533-easy-concat

Back

type Concat<T extends readonly  any[], U extends readonly  any[]> = [...T, ...U]

Solution by horoshopodumaj #37996


Version 1 - Explicit tuple-aware concatenation

// A helper type representing any readonly tuple or array.
// Used to check whether a value already behaves like a tuple.
type Tuple = readonly unknown[]

// Concat merges two values while keeping tuple semantics intact.
//
// How it behaves:
// 1. tuple + tuple
//    → both tuples are spread and merged into one
//
// 2. tuple + non-tuple
//    → the value is appended to the end of the tuple
//
// 3. non-tuple + tuple
//    → the value is prepended to the beginning of the tuple
//
// 4. non-tuple + non-tuple
//    → both values are wrapped into a new tuple
//
// This ensures:
// - order is preserved
// - tuple length is accurate
// - literal types are not widened
type Concat<Left, Right> =
  Left extends Tuple
    ? Right extends Tuple
      ? [...Left, ...Right]
      : [...Left, Right]
    : Right extends Tuple
      ? [Left, ...Right]
      : [Left, Right]

Version 2 - Normalize first, then concatenate

// Converts any input into a tuple form.
//
// Behavior:
// - If the input is already a tuple, keep it as-is
// - If the input is a single value, wrap it in a tuple
//
// This gives us a consistent shape to work with
// and avoids branching logic later.
type AsTuple<Value> =
  Value extends readonly unknown[] ? Value : [Value]

// Concat works by first normalizing both inputs into tuples
// and then merging them using tuple spread syntax.
//
// Because both sides are guaranteed to be tuples at this point:
// - order is preserved
// - tuple length is computed correctly
// - literal types remain intact
//
// This approach favors composition over conditional branching
// and is easier to read, reuse, and maintain.
type Concat<Left, Right> = [
  ...AsTuple<Left>,
  ...AsTuple<Right>
]

Solution by Muntazir-sd #37955

// 解答をここに記入
type Concat<T extends readonly any[], U extends  readonly any[]> = [...T,...U]

Solution by tac-tac-go #37903

// your answers
type Concat<T extends any[], U extends any[]> = [...T, ...U];

Solution by istamal #37897

type Concat<T extends readonly unknown[], U extends readonly unknown[]> = [...T, ...U];

Solution by Hansol46 #37836

type Concat<A1 extends readonly any[], A2 extends readonly any[]> = [...A1, ...A2];

Solution by pkutsenko #37828

// your answers

Solution by Brahima-Fofana #37762

type Concat<T, U> = T extends readonly unknown[]
  ? U extends readonly unknown[]
    ? [...T, ...U]
    : T
  : U extends readonly unknown[]
    ? U
    : []

Solution by rauf322 #37704

// your answers
type Concat<T extends readonly unknown[], U extends readonly unknown[]> = [...T, ...U]

Solution by AlexanderNP #37668

type Concat<T extends readonly unknown[], U extends readonly unknown[]> = [...T, ...U]

// your answers

Solution by AndSLASH #37663

type Concat<T extends readonly any[] , U extends readonly any[]> = [...T , ...U]

Solution by BernTheSeal #37657

type Concat<T extends readonly any[], U extends readonly any[]> = [...T, ...U]

Solution by tomo-local #37580

// your answers

type Concat<T, U> = T extends readonly any[] ? U extends readonly any[] ? [...T,...U] : never : error

Solution by PAVANT009 #37567

type Concat<T extends readonly unknown[], U extends readonly unknown[]> = [...T, ...U]


In plain words

Concat<T, U> constructs a new tuple type by:

  1. Taking all elements from tuple T.
  2. Adding all elements from tuple U.
  3. Producing a new tuple type that combines them in order.

✅ Example:

type A = [1, 2]
type B = ["a", "b"]

type Result = Concat<A, B>
// Result: [1, 2, "a", "b"]

So Result is exactly [1, 2, "a", "b"]. 🎯

Solution by 3aluw #37460

type Concat<T extends readonly any[], U extends readonly any[]> = [...T, ...U]; 

Solution by Nakamura25257 #37261

type Tuple = readonly unknown[];
type Concat<T extends Tuple, U extends Tuple> = [...T, ...U];

Solution by djdidi #37133

type Concat<T extends readonly any[], U  extends readonly any[]> = [...T, ...U]

Solution by 359Steve #37018

type Tuple= readonly unknown[]

type Concat<T extends Tuple, U extends Tuple> = [...T,...U]

Solution by Anonymous961 #36973

// 你的答案

type Concat<T extends readonly any[], U extends readonly any[]> = [...T, ...U];

Solution by 180ParkerLi #36923

// your answers
type Concat<T extends readonly any[], U extends readonly any[]> = [...T, ...U];

Solution by AlexBraunMagic #36914

type Concat<T extends readonly unknown[], U extends readonly unknown[]> = [...T, ...U];

Solution by shaishab316 #36846

type Result = Concat<[1], [2]> // expected to be [1, 2]
// 你的答案
type Concat<T extends readonly any[], U extends readonly any[]> = [...T,...U]

Solution by mola-fish #36833

type Concat<T extends any[], U extends any[]> = [...T, ...U];

/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

const tuple = [1] as const

type cases = [
  Expect<Equal<Concat<[], []>, []>>,
  Expect<Equal<Concat<[], [1]>, [1]>>,
  Expect<Equal<Concat<typeof tuple, typeof tuple>, [1, 1]>>,
  Expect<Equal<Concat<[1, 2], [3, 4]>, [1, 2, 3, 4]>>,
  Expect<Equal<Concat<['1', 2, '3'], [false, boolean, '4']>, ['1', 2, '3', false, boolean, '4']>>,
]

// @ts-expect-error
type error = Concat<null, undefined>

Solution by AnastasiaSv #36766

type Concat<T extends readonly unknown[], U extends readonly unknown[]> = [...T, ...U]

Solution by Abdullah-Elsayed01 #36752

type Concat<T extends readonly any[], U extends readonly any[]> = [...T, ...U]

Solution by tungulin #36717

type Concat<T extends T[], U extends U[]> = [...T, ...U]

Solution by Mamdouhreda #36709

// 여기 풀이를 입력하세요
type Concat<T extends unknown[], U extends unknown[]> = [...T, ...U]

Solution by seungdeok #36667

type Tuple = typeof tuple | any[]
type Concat<T extends Tuple, U  extends Tuple> = [...T, ...U]

Solution by wiJeeXu #36650

// 你的答案
type Concat<T extends any[] | readonly any[], U extends any[] | readonly any[]> = [...T, ...U]

Solution by MrSissel #36585

type Concat<T extends any[], U extends any[]> = [...T, ...U]

Solution by ChemieAi #36554