"매 compile-time 매 immutability marker — runtime enforcement 의 X". 매 TS 의 의 의 mutation prevention 의 의 매 type-system level — readonly keyword, Readonly<T>, ReadonlyArray<T>, as const 매 4 가지 form. 매 runtime freeze 가 의 매 Object.freeze 의 의.
Mapped type: Readonly<T> — all props readonly (shallow).
Array variants: readonly T[] 또는 ReadonlyArray<T> — no .push, .pop, etc.
as const: literal narrowing + deep readonly tuple/object literal.
매 핵심 행동
매 compile-time only — JS runtime 의 의 effect 의 X.
Variance: T[] is assignable to readonly T[] (covariant), but not vice-versa.
Shallow: Readonly<{ a: { b: 1 } }> 매 a is readonly, a.b 의 X.
매 응용
Public API: function param 매 readonly T[] — caller 의 mutation 의 의 의 의.
Redux / Zustand state — 매 immutability invariant.
Config object — as const 매 literal type.
Tuple destructuring — const point = [1, 2] as const.
Discriminated union literals — type Status = "ok" | "fail" via as const.
💻 패턴
Property readonly
interfaceUser{readonlyid: string;name: string}constu: User={id:"1",name:"A"};u.name="B";// OK
u.id="2";// Error: cannot assign to 'id' because it is a read-only