"매 Readonly<T> 의 surface-only — 매 nested mutation 가 still possible". 매 DeepReadonly 가 매 recursively 의 every property 의 readonly 의 mark — 매 redux state, config object, frozen domain model 의 essential. TS 5.x 의 매 const type parameter + DeepReadonly 가 매 powerful combo.
매 핵심
매 vs Readonly
Readonly<T> — 매 top-level only. 매 obj.nested.mutate = X 가 still allowed.
DeepReadonly<T> — 매 every level 의 recursive freeze.
매 type-only vs runtime
DeepReadonly 의 매 compile-time type guarantee — 매 runtime 의 mutation 의 X protect.
매 runtime freeze 의 Object.freeze (shallow) 또는 deepFreeze helper 사용.
TypeScript 5.0+ as const 의 매 literal-level deep readonly 의 produce.
constconfig={features:{newCheckout: true,beta:['user1','user2']},limits:{rpm: 100},}asconst;// 매 `as const` 가 매 every literal 의 deeply readonly + literal-typed 로 만듦.
typeConfig=typeofconfig;// Config['features']['beta'] === readonly ['user1', 'user2']
언제: 매 utility type 의 design / extension, 매 type error explanation, 매 readonly violation 의 codemod 작성.
언제 X: 매 runtime data validation (Zod 사용). 매 hot-path performance tuning (TS types 가 erased — runtime cost 0).
❌ 안티패턴
함수 type 의 readonly 적용: 매 (...args) => any 가 readonly 의 의미 X — special-case 필요.
Date / RegExp 의 recurse: 매 built-in instances 가 깨짐 — exclude 의 type guard.
DeepReadonly + cast away: state as Mutable 가 매 type safety 의 destroy.
Runtime mutation through cast: 매 (state as any).x = 1 — 매 type lie 의 propagate.
Naive keyof T on union: distributive conditional 의 사용.