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?

type-challenges解いてみた 初級 3057 Push

Last updated at Posted at 2025-06-01

問題

Array.pushのジェネリックバージョンを実装します。

例えば:

```typescript
type Result = Push<[1, 2], '3'> // [1, 2, '3']
```

回答

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

ちょっと解説

T extends unknown[]

こうかくことで、T を配列/タプルに限定することができます。
よって後続で[...T, U]スプレッドを使用することができます。

...T

Variadic Tuple Typesと呼ばれるものです。
JSのスプレッド構文の型バージョンであり、ある要素を別のタプル型に埋め込むことができます。

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?