这是indexloc提供的服务,不要输入任何密码
Skip to content

533 - Concat (🎥 Video Explanation and Solution) #21538

@dimitropoulos

Description

@dimitropoulos

533 - Concat

Concat is the first in a series of challenges that look at common array manipulation techniques. There are many more to come, but it starts out small with this challenge.

🎥 Video Explanation

Release Date: 2023-01-10 19:00 UTC

Concat

🔢 Code

import type { Equal, Expect } from './test-utils'

type A1 = Concat<[], []>;
type B1 = [];
type C1 = Expect<Equal<A1, B1>>;

type A2 = Concat<[], [1]>;
type B2 = [1];
type C2 = Expect<Equal<A2, B2>>;

type A3 = Concat<[1, 2], [3, 4]>;
type B3 = [1, 2, 3, 4];
type C3 = Expect<Equal<A3, B3>>;

type A4 = Concat<['1', 2], [false, boolean, '4']>;
type B4 = ['1', 2, false, boolean, '4'];
type C4 = Expect<Equal<A4, B4>>;

// ============= Your Code Here =============
type Concat<
  T extends unknown[],
  U extends unknown[]
> = [...T, ...U];

// ============== Alternatives ==============
// this is bad
// because it won't error if you pass a non-array
type Concat<T, U> =
  T extends unknown[]
  ? U extends unknown[]
    ? [...T, ...U]
    : never
  : never;

➕ More Solutions

For more video solutions to other challenges: see the umbrella list! #21338

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions