0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Qiita全国学生対抗戦Advent Calendar 2024

Day 10

【TypeScript】type-challenges 初級編 3060・Unshift 解説

Last updated at Posted at 2024-12-13

お題

Array.unshiftの型バージョンUnshiftを実装する(配列の先頭に要素を追加する)。

やりたいこと

type Result = Unshift<[1, 2], 0>;  // => [0, 1, 2]

解答

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

解説

処理の流れ

  • <T extends unknown[], U>
    Tに配列を受け取るように制限。Uは先頭に追加する要素を受け取る。
  • [U, ...T]
    Uを先頭に置いて、スプレッド構文でTの要素を展開し、[]でラッピングする。

スプレッド構文とは...

今回の問題

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?