問題一覧に戻る
初級基本
問題10: リテラル型

リテラル型を使って、特定の値のみを受け入れる型を定義する方法を学びます。文字列、数値、真偽値のリテラルを組み合わせます。

// 文字列リテラル型
type Status = | | ;
let currentStatus: Status = "success";

// 数値リテラル型(1〜6)
type DiceValue = | | | | | ;
let dice: DiceValue = 3;

// 真偽値リテラル型
type YesOrNo = | ;
let answer: YesOrNo = true;