2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

空ではない配列型を作成する方法

Posted at

空ではない配列型の作成

配列型(例えば、Array<string>)はデフォルトで空の配列を許可します。しかし、配列が空で宣言されないようにする静的型チェックを行うカスタム配列型を定義することができます。

これを実現するために、配列型には少なくとも1つのアイテムがあることを指定し、さらに残りの配列の要素をスプレッド演算子で表現します。

type NonEmptyArray<T> = [T, ...T[]];

アイテムがない NonEmptyArray を宣言する場合

以下のように NonEmptyArray をアイテムなしで宣言すると:

const statuses: NonEmptyArray<string> = [];

次のような型エラーが発生します:

Type '[]' is not assignable to type 'NonEmptyArray<string>'.
  Source has 0 element(s) but target requires 1.

少なくとも1つのアイテムを追加すると型エラーが解消される

一方で、少なくとも1つのアイテムを追加すると、型エラーは解消されます。

const statuses: NonEmptyArray<string> = ['active'];

この型の TS Playground の例 はこちらです。

フリーランスエンジニア必見!

最後に、フリーランスエンジニアの方にご案内です。
あなたに今だけご紹介できる”エンド直”・”高単価”の案件があります!

気になる方は公式ラインの追加をお願いします👇
https://s.lmes.jp/landing-qr/2005758918-ADDegZkx?uLand=tau44P

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?