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解いてみた 初級 533 Concat

Posted at

問題


JavaScript の`Array.concat`関数を型システムに実装します。この型は 2 つの引数を受け取り、受け取ったイテレータの要素を順に含む新しい配列を返します。

例えば:

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

回答

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

ちょっと解説

readonly any[]

readonlyをつけておくことでミュータブル/イミュータブル両方の配列を受け取れます。
例えば...

  • [1, 2, 3] / readonly [1, 2, 3]

などの組み合わせをすべて許可できます。

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?