読み方のヒント
矢印ボタン(または ← → キー)でページを移動できます。
値がないことを表す型もあります。
let pickedUpAt: string | null = null;
pickedUpAt = "2026-11-02 12:35";
| 型 | 意味 |
|---|---|
undefined | まだ値が入っていない |
null | 値がないことを、わざと表す |
string | null は、「文字列か null のどちらか」という意味です。| で型をつなぐ書き方をユニオン型と呼び、この章の続き『TS入門 ユニオン型と型の絞り込み』で詳しく扱います。
この章の設定(strict)では、string の型の変数に null を入れるとエラーになります。値がないことがある変数は、| null を付けて、そのことを型で示します。
const name: string = null;
Type 'null' is not assignable to type 'string'.
const maxQuantity: number = 10;const reservable: boolean = true;let cancelledAt: string | null = null;3 / 17