Skip to main content

Concat

introduction#

Implement the JavaScript Array.concat function in the type system. A type takes the two arguments. The output should be a new array that includes inputs in ltr order

For example

ts
type Result = Concat<[1], [2]>; // expected to be [1, 2]
ts
type Result = Concat<[1], [2]>; // expected to be [1, 2]
View on GitHub

start point#

ts
/* _____________ Your Code Here _____________ */
type Concat<T, U> = any;
 
/* _____________ Test Cases _____________ */
type cases = [
Expect<Equal<Concat<[], []>, []>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Concat<[], [1]>, [1]>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Concat<[1, 2], [3, 4]>, [1, 2, 3, 4]>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<
Equal<
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Concat<['1', 2, '3'], [false, boolean, '4']>,
['1', 2, '3', false, boolean, '4']
>
>
];
Try
ts
/* _____________ Your Code Here _____________ */
type Concat<T, U> = any;
 
/* _____________ Test Cases _____________ */
type cases = [
Expect<Equal<Concat<[], []>, []>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Concat<[], [1]>, [1]>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Concat<[1, 2], [3, 4]>, [1, 2, 3, 4]>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<
Equal<
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Concat<['1', 2, '3'], [false, boolean, '4']>,
['1', 2, '3', false, boolean, '4']
>
>
];
Try
take the challengetake the challenge

my solution#

Spoiler warning // Click to reveal answer
ts
type Concat<T extends any[], U extends any[]> = [...T, ...U];
Try
ts
type Concat<T extends any[], U extends any[]> = [...T, ...U];
Try
view more solutions