type-challenges: 533 - Concat

type Tuple = readonly unknown[];

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

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 56
  • Comments: 18

Most upvoted comments

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

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

The only issue with this is that any[] says “any list type that is not readonly”, so this will throw an error:

Expect<Equal<Concat<readonly [], []>, []>>,

Type 'readonly []' does not satisfy the constraint 'any[]'. The type 'readonly []' is 'readonly' and cannot be assigned to the mutable type 'any[]'. ts(2344)

Can somebody provide a reference to docs that explain using the spread operator on types?