TypeScriptのconstアサーション(as const)

できるだけ狭い範囲の型を定義したいときにconstアサーションが使える

オブジェクト末尾に as_const を書くとプロパティがreadonlyになり、リテラルタイプで指定した物と同じ扱いになる

const user = {
    id: 14,
    name: 'alice',
    age: 20
} as const;
// 末尾にas constを付けることでuser.idの型はnumberではなく14、user.nameの型はstringではなく'alice'になる

user.name = 'bob'
// Cannot assign to 'name' because it is a read-only property.