📌 1. 概要


TypeScript では、オブジェクトのプロパティ(キーと値のペア)に対して

というルールを付けられます。

これにより、値の書き換えを防いだり、値を必ずしも与えなくてよくしたりできます。(TypeScript)


🗣️ 2. 初学者向けに一言で説明


💡 3. 具体例

🔹 readonly の例

interface User {
  readonly id: number;
  name: string;
}

const user: User = { id: 1, name: "Alice" };

user.name = "Bob";   // OK: name は書き換え可能
user.id = 2;        // ❌ コンパイルエラー: id は readonly

🔹 optional の例

interface Options {
  width?: number;
  height?: number;
}

const o1: Options = {};
const o2: Options = { width: 100 };   // height はなくても OK


🧠 4. 具体例に対する解説

🔸 readonly の解説