問題一覧に戻る
初級特殊な型
問題15: constアサーション
as constを使って、リテラル型を作成する方法を学びます。値を読み取り専用にし、より厳密な型を生成します。設定オブジェクトや定数の定義、タプルの作成などで活用され、コードの意図を明確にします。
// 配列のconstアサーション
const colors = ["red", "green", "blue"] ;
// オブジェクトのconstアサーション
const config = {
host: "localhost",
port: 3000,
debug: true
} ;
// ネストしたオブジェクトのconstアサーション
const settings = {
theme: {
primary: "#007bff",
secondary: "#6c757d"
}
} ;