constアサーション「as const」 (const assertion)
オブジェクトリテラルの末尾にas constを記述すればプロパティがreadonlyでリテラルタイプで指定した物と同等の扱いになります。
tsconstpikachu = {name : "pikachu",no : 25,genre : "mouse pokémon",height : 0.4,weight : 6.0,} asconst ;
tsconstpikachu = {name : "pikachu",no : 25,genre : "mouse pokémon",height : 0.4,weight : 6.0,} asconst ;
代入はもちろんできません。
tsCannot assign to 'name' because it is a read-only property.2540Cannot assign to 'name' because it is a read-only property.pikachu .= "raichu"; name
tsCannot assign to 'name' because it is a read-only property.2540Cannot assign to 'name' because it is a read-only property.pikachu .= "raichu"; name
readonlyとconst assertionの違い
どちらもオブジェクトのプロパティをreadonlyにする機能は同じですが、以下が異なります。
readonlyはプロパティごとにつけられる
const assertionはオブジェクト全体に対する宣言なので、すべてのプロパティが対象になりますが、readonlyは必要なプロパティのみにつけることができます。
const assertionは再帰的にreadonlyにできる
オブジェクトの中にオブジェクトがあるときの挙動が異なります。たとえば次のようなオブジェクトがあるとします。
tstypeCountry = {name : string;capitalCity : string;};typeContinent = {readonlyname : string;readonlycanada :Country ;readonlyus :Country ;readonlymexico :Country ;};constamerica :Continent = {name : "North American Continent",canada : {name : "Republic of Canada",capitalCity : "Ottawa",},us : {name : "United States of America",capitalCity : "Washington, D.C.",},mexico : {name : "United Mexican States",capitalCity : "Mexico City",},};
tstypeCountry = {name : string;capitalCity : string;};typeContinent = {readonlyname : string;readonlycanada :Country ;readonlyus :Country ;readonlymexico :Country ;};constamerica :Continent = {name : "North American Continent",canada : {name : "Republic of Canada",capitalCity : "Ottawa",},us : {name : "United States of America",capitalCity : "Washington, D.C.",},mexico : {name : "United Mexican States",capitalCity : "Mexico City",},};
ここでContinentのタイプエイリアスがもつプロパティはすべてreadonlyです。よって次のようなことはできません。
tsCannot assign to 'name' because it is a read-only property.2540Cannot assign to 'name' because it is a read-only property.america .= "African Continent"; name Cannot assign to 'canada' because it is a read-only property.2540Cannot assign to 'canada' because it is a read-only property.america .= { canada name : "Republic of Côte d'Ivoire",capitalCity : "Yamoussoukro",};
tsCannot assign to 'name' because it is a read-only property.2540Cannot assign to 'name' because it is a read-only property.america .= "African Continent"; name Cannot assign to 'canada' because it is a read-only property.2540Cannot assign to 'canada' because it is a read-only property.america .= { canada name : "Republic of Côte d'Ivoire",capitalCity : "Yamoussoukro",};
しかしながら、次のようなことは問題なくできてしまいます。
tsamerica .canada .name = "Republic of Côte d'Ivoire";america .canada .capitalCity = "Yamoussoukro";
tsamerica .canada .name = "Republic of Côte d'Ivoire";america .canada .capitalCity = "Yamoussoukro";
これはreadonlyをつけたプロパティがオブジェクトである場合に、そのオブジェクトのプロパティまでreadonlyにはしないことに起因します。
const assertionはすべてのプロパティを固定する
as constを付けます。
tsconstamerica = {name : "North American Continent",canada : {name : "Republic of Canada",capitalCity : "Ottawa",},us : {name : "United States of America",capitalCity : "Washington, D.C.",},mexico : {name : "United Mexican States",capitalCity : "Mexico City",},} asconst ;
tsconstamerica = {name : "North American Continent",canada : {name : "Republic of Canada",capitalCity : "Ottawa",},us : {name : "United States of America",capitalCity : "Washington, D.C.",},mexico : {name : "United Mexican States",capitalCity : "Mexico City",},} asconst ;
readonlyと同様にトップレベルのプロパティへの代入はできません。
tsCannot assign to 'name' because it is a read-only property.2540Cannot assign to 'name' because it is a read-only property.america .= "African Continent"; name Cannot assign to 'canada' because it is a read-only property.2540Cannot assign to 'canada' because it is a read-only property.america .= { canada name : "Republic of Côte d'Ivoire",capitalCity : "Yamoussoukro",};
tsCannot assign to 'name' because it is a read-only property.2540Cannot assign to 'name' because it is a read-only property.america .= "African Continent"; name Cannot assign to 'canada' because it is a read-only property.2540Cannot assign to 'canada' because it is a read-only property.america .= { canada name : "Republic of Côte d'Ivoire",capitalCity : "Yamoussoukro",};
これだけではなくオブジェクトが持つプロパティも同様にreadonlyにしてくれます。
tsCannot assign to 'name' because it is a read-only property.2540Cannot assign to 'name' because it is a read-only property.america .canada .= "Republic of Côte d'Ivoire"; name Cannot assign to 'capitalCity' because it is a read-only property.2540Cannot assign to 'capitalCity' because it is a read-only property.america .canada .= "Yamoussoukro"; capitalCity
tsCannot assign to 'name' because it is a read-only property.2540Cannot assign to 'name' because it is a read-only property.america .canada .= "Republic of Côte d'Ivoire"; name Cannot assign to 'capitalCity' because it is a read-only property.2540Cannot assign to 'capitalCity' because it is a read-only property.america .canada .= "Yamoussoukro"; capitalCity
関連情報
📄️ 型アサーション「as」
TypeScriptには、型推論を上書きする機能があります。その機能を型アサーション(type assertion)と言います。
📄️ オブジェクト型のreadonlyプロパティ
TypeScriptでは、オブジェクトのプロパティを読み取り専用にすることができます。読み取り専用にしたいプロパティにはreadonly修飾子をつけます。読み取り専用のプロパティに値を代入しようとすると、TypeScriptコンパイラーが代入不可の旨を警告するようになります。