TypeScript オブジェクトをreadonlyにする（備忘録） 

# 結論
readonlyではない型注釈をした変数に、readonlyなオブジェクトを代入するとプロパティの値の変更ができてしまう。
但し、Object.freeze()でオブジェクトを作成した場合は実行時エラーになる。
オブジェクトを定数として扱うという用途で考えるとas constで十分その要件は満たせると思う。
次回は型注釈の仕組みについて調べたい。

理解を深める為、自分で検証してみました。
参考になる記事を書いてくださった方々に感謝です！

# 前提
1.以下3つのパターンを使って検証します。
- Readonly
- as const
- Object.freeze()

2.代入方法
- readonlyではない型注釈(type annotation; 型アノテーション)した変数に代入
- スプレッドオペレーター
- Object.assign()
- Object.create()
- JSON.parse(JSON.stringify())

3.代入先のオブジェクトの確認内容
プロパティの値の書き換えのみとします。理由は以下です。
- プロパティの追加はTypeScriptだとコンパイルエラーになる
- プロパティがreadonlyならプロパティの変更は不可能なので、削除と値の書き換えが可能かという結果は同じ
- プロパティの列挙可否は今回の観点としていない。

# Readonly
```ts:Readonly
type ObjType = {
  name: string;
  o: { hoge: string }
}

type ReadonlyObjType = Readonly<ObjType>;

const obj: ReadonlyObjType = {
  name: "Taisei Yamane",
  o: { hoge: "fuga" }
};

obj.o.hoge = "piyo";
obj.name = "";
//Cannot assign to 'name' because it is a read-only property.

const copyObj: ObjType = obj;
copyObj.name = "";
copyObj.o.hoge = "piyo";

const copyObj2 = { ...obj };
copyObj2.name = "";
copyObj2.o.hoge = "piyo";

const copyObj3 = Object.assign({}, obj);
copyObj3.name = "";
//Cannot assign to 'name' because it is a read-only property.ts(2540)
copyObj3.o.hoge = "piyo";

type ObjType = {
  name: string;
  o: { hoge: string }
}

type ReadonlyObjType = Readonly<ObjType>;

const obj: ReadonlyObjType = {
  name: "Taisei Yamane",
  o: { hoge: "fuga" }
};

obj.o.hoge = "piyo";
obj.name = "";
//Cannot assign to 'name' because it is a read-only property.

const copyObj: ObjType = obj;
copyObj.name = "";
copyObj.o.hoge = "piyo";

const copyObj2 = { ...obj };
copyObj2.name = "";
copyObj2.o.hoge = "piyo";

const copyObj3 = Object.assign({}, obj);
copyObj3.name = "";
//Cannot assign to 'name' because it is a read-only property.ts(2540)
copyObj3.o.hoge = "piyo";

const copyObj4 = Object.create(obj)
copyObj4.name = "";
copyObj4.o.hoge = "piyo";

const copyObj5 = JSON.parse(JSON.stringify(obj))
copyObj5.name = "";
copyObj5.o.hoge = "piyo";

Object.defineProperty(obj, 'name', {
  value: '',
});
```
https://www.typescriptlang.org/play?#code/C4TwDgpgBA8gRgKwCrmgXigbwFBSgOwEMBbCALigGdgAnAS3wHMBuXKAewsygAt3HyVWg0ZQAvtgnZQkKACUIhACbt8AGxDxkqKBgXLVGgDxaUkAHytsAY1XUOiCvpXrNiM+ixsipCgCIkQjpKCDooAE0SQnwIPwAaNk4sXn5BPwAzAFdGQj9xSSt2RAA6dmK+AV0oPzA6EHY-ViKEYp9PP0bsAHougGFo-HZgKEJKSjpGfChgdigAcja5qDgIa0JMkKg6YeCRqBpFJQBaQxAoMBp2SBpQYuwbO2HbMDcEClMdDGbWZ9fWknanV+WlK5VSVRqdQaVls+HswMQACYqtximjmuIfldXoj-qQIUDsVpcWUKu1avVOg84U8iYgAMxVLSrYDFUbjSYACkwYjiDgQAEosS8tPS8YDWD1+vhBsN2RMpjN5otlqt1pttltKHsDsoTq5zpdrrdgJROYiAKwAFgADAKHiKGaCyRCKdD7rD4XSEFamYgWcVrLrgBBOc17QifeKCcLXlbneCMJDKTDHlBIxaqgApADKMAAcsUwIQaCFObmC8VqPQmHR0iAw4gBRHvRbo0nCY6EG3SYnqm6qczrKylBB0gwIAAFI0QG4N5p8hYAuZ8nB4ABuhDUmUEcxXkgFQA
# as const
```ts:as const
type ObjType = {
  name: string;
  o: { hoge: string }
}

const obj = {
    name: "Taisei Yamane",
    o:{hoge:"fuga"}
} as const;


obj.o.hoge = "piyo";
//Cannot assign to 'name' because it is a read-only property.
obj.name = "";
//Cannot assign to 'name' because it is a read-only property.

const copyObj:ObjType = obj;
copyObj.name = "";
copyObj.o.hoge = "piyo";

const copyObj2 = {...obj};
copyObj2.name = "";
//Type '""' is not assignable to type '"Taisei Yamane"'.
copyObj2.o.hoge = "piyo";
//Cannot assign to 'hoge' because it is a read-only property.

const copyObj3 = Object.assign({},obj);
copyObj3.name = "";
//Cannot assign to 'name' because it is a read-only property.
copyObj3.o.hoge = "piyo";
//Cannot assign to 'hoge' because it is a read-only property.

const copyObj4 = Object.create(obj)
copyObj4.name = "";
copyObj4.o.hoge = "piyo";

const copyObj5 = JSON.parse(JSON.stringify(obj))
copyObj5.name = "";
copyObj5.o.hoge = "piyo";

Object.defineProperty(obj, 'name', {
  value: '',
});
```
https://www.typescriptlang.org/play?#code/C4TwDgpgBA8gRgKwCrmgXigbwFBSgOwEMBbCALigGdgAnAS3wHMBuXKAewsygAt3HyVWg0ZQAvtgnYAxu3zUOiKBhx48RUhQBESQnUoQ6UAJolC+CFoA0bPJ0x8BZLQDMAro0JaJYqIUpQsvLArNjY7IgAdOyRjuhQWmB0IOxarAD06QDC5vjswH6UlHSM+FDA7FAA5BoQVVBwENKEbgZQdAX6flA0EIQAJgC0cgA2IFBgNOyQNKCR4VG1yglp2Jk5+HkF-sWl5ZU1JHUNTS1tHe0BhD19Q6Pjk9MQsyDzMnIKsmAg8AhkvyhIMsIghWF8fosjsstKtwb9orF+PFEslUqEgp9phCEAAmZaYSKEkFiMFY344yJLDAwjLpQHQKow+pdLaFXZEOAjaAVcqoao6PQGIymYjmSxVeZwxAUmJxaFJFKrda5fJskplHlVOL1RrNVrQC5da69AbDfBjCZTGZzMIYgpShAAZmWvyawEiO3VAApMGIrCCAJSk76-R2UqHUpXZFXbIrq-bVWo60769qdK43U33S1PF6SsmIMOypHy1FRjasz17TXak5687p7omu7mh5W54297BQIFhAAFhdiDdkWkJuAEC9gfeIcQffDpGhsN7c+LAlLivRH3tvYArMsAFIAZRgADlImBCDQDF6j6fItR6Ew6C4QJPEAGA9PsTv58ilzOEB-VdkQVNEwldaR3X6CAXAYCAAAV2xeN8ECsRMjiqNDVCgAA3QgRjcQQqkwyQgyAA


# Object.freeze()
```ts:Object.freeze()
type ObjType = {
  name: string;
  o: { hoge: string }
}

const obj = Object.freeze({
  name: "Taisei Yamane",
  o: { hoge: "fuga" }
});

obj.o.hoge = "piyo";
obj.name = "";
//Cannot assign to 'name' because it is a read-only property.

const copyObj: ObjType = obj;
copyObj.name = "";
copyObj.o.hoge = "piyo";
//実行時エラー

const copyObj2 = { ...obj };
copyObj2.name = "";
//Type '""' is not assignable to type '"Taisei Yamane"'.
copyObj2.o.hoge = "piyo";

const copyObj3 = Object.assign({}, obj);
copyObj3.name = "";
//Cannot assign to 'name' because it is a read-only property.
copyObj3.o.hoge = "piyo";

const copyObj4 = Object.create(obj)
copyObj4.name = "";
copyObj4.o.hoge = "piyo";
//実行時エラー

const copyObj5 = JSON.parse(JSON.stringify(obj))
copyObj5.name = "";
copyObj5.o.hoge = "piyo";

Object.defineProperty(obj, 'name', {
  value: '',
});
//実行時エラー

```
https://www.typescriptlang.org/play?#code/C4TwDgpgBA8gRgKwCrmgXigbwFBSgOwEMBbCALigGdgAnAS3wHMBuXKAewsygAt3HyVWg0ZQAvtgnYAxu3zUOiKBngII04ADoAZjQgQAXhAAUOPEVIUAREkJ1KEOlACaJQvghWANG05Ze-IJW2gCujIRW4pIAlKzY7Iia7Jp8AspQVmB0IOxWrAkImhboGXnYAPTlAMLu+OzAUISUlHSM+FDA7FAA5MXdUHDqhCEOUHQN9o1QeoQAJgC0cgA2IFBgNOyQNKCa2DJyCrJgIKoUqiiQ6QWsRyeJxelWZbeqSSmBj1k5ZfvyDS+IABM6W4mjBBXEN02dwQgKKJBKT1YlQu0G6T36kzqDSaLTahDgS2gnQ6qB6NjsDicrmI7k83V2ANhb1SiK+uTisj+UCZAGZ0qp1Fpca18KYxF5FAhYvtjqpefDSI8ypUavhsY1mqKOl1egj+oNpMNRuMxpQpjMFstVutNhBtiBGdD5SyPhhMtkOXsuYdnYgACwCxBCzTSGbAEwFaKymH+xWI55+hBx5Ksz6en4+-5JgCs6QAUgBlGAAOU0YEINAcxiLpc01HoTDo2hAxij0aZOfjyqhcsQXdTboy7J+go0mlmEG0DAgAAUNltQG3EJK9aRupKzFAAG6EJYhQTdDcxVhAA

# 参考）
https://chaika.hatenablog.com/entry/2020/08/28/083000

https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze

https://www.geeksforgeeks.org/how-to-deep-freeze-an-object-in-javascript/

# 宣伝させてください！
https://recruit.iimon.co.jp/