docs(10_Wiki): 위키 전체 재구성 — Topic_* 폴더를 4개 카테고리로 통합 + 대규모 중복 제거
Topic_Agent/Topic_Blog/Topics/Topics_Biz/Topics_Meeting/Topics_Rag의 마크다운 지식 문서를 Topic_General/Topic_Programming/Topic_Graphic/Topic_Business 4개 카테고리로 재분류. - 중복 제거: frontmatter의 status:duplicate/merged + duplicate_of/redirect_to 필드로 자기 자신을 중복으로 선언한 리다이렉트 stub 1032개 제거, 완전 동일 내용 파일 472개 제거, 동일 파일명·다른 내용 충돌 시 더 큰(완전한) 버전만 유지(162개 제거) — 총 1639개 중복 제거. - 분류: 폴더 단위로 명확한 항목(AI_and_ML/Coding/Architecture 등 → Programming, Comfyui/Visual_Effects → Graphic, Topics_Biz/Topics_Meeting/사업 등 → Business, Poetic_Blog_Writing/창의성/Game_Design 등 → General)은 폴더 우선순위로, 나머지 혼재 폴더(Topic_Agent/Topic_Blog/Topics 루트/Thinking & Reasoning/Other/UI_UX_Assets)는 title/tags 키워드 스코어링으로 파일 단위 분류(불명확한 경우 General로 폴백). 원본 폴더명은 "From_*" 서브폴더로 보존해 추적 가능성 유지. - 최종 배치: Programming 2784 / General 1608 / Graphic 285 / Business 249 = 4926개 문서. - 에이전트 운영 상태(.astra/.agent/.obsidian/sessions/memory/_company/docs/lessons/_shared/src)는 지식 콘텐츠가 아니므로 재분류 대상에서 제외하고 원위치 유지. - Topics/Topic_email(상위 보호 폴더 Topic_email과 파일명 100% 중복) 삭제 — 보호 폴더 자체는 미변경. - 완전히 비게 된 Topic_Agent/Topic_Blog/Topics_Biz/Topics_Rag 폴더 제거.
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
---
|
||||
id: wiki-2026-0508-readonly
|
||||
title: TypeScript readonly
|
||||
category: 10_Wiki/Topics
|
||||
status: verified
|
||||
canonical_id: self
|
||||
aliases: [readonly, Readonly<T>, ReadonlyArray, as const]
|
||||
duplicate_of: none
|
||||
source_trust_level: A
|
||||
confidence_score: 0.9
|
||||
verification_status: applied
|
||||
tags: [typescript, immutability, types]
|
||||
raw_sources: []
|
||||
last_reinforced: 2026-05-10
|
||||
github_commit: pending
|
||||
tech_stack:
|
||||
language: typescript
|
||||
framework: typescript
|
||||
---
|
||||
|
||||
# TypeScript readonly
|
||||
|
||||
## 매 한 줄
|
||||
> **"매 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` 의 의.
|
||||
|
||||
## 매 핵심
|
||||
|
||||
### 매 4 가지 form
|
||||
- **Property modifier**: `interface User { readonly id: string }` — assign once.
|
||||
- **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.
|
||||
|
||||
### 매 응용
|
||||
1. Public API: function param 매 `readonly T[]` — caller 의 mutation 의 의 의 의.
|
||||
2. Redux / Zustand state — 매 immutability invariant.
|
||||
3. Config object — `as const` 매 literal type.
|
||||
4. Tuple destructuring — `const point = [1, 2] as const`.
|
||||
5. Discriminated union literals — `type Status = "ok" | "fail"` via `as const`.
|
||||
|
||||
## 💻 패턴
|
||||
|
||||
### Property `readonly`
|
||||
```ts
|
||||
interface User { readonly id: string; name: string }
|
||||
const u: User = { id: "1", name: "A" };
|
||||
u.name = "B"; // OK
|
||||
u.id = "2"; // Error: cannot assign to 'id' because it is a read-only
|
||||
```
|
||||
|
||||
### `Readonly<T>` mapped
|
||||
```ts
|
||||
type Frozen<T> = Readonly<T>;
|
||||
const cfg: Frozen<{ host: string; port: number }> = { host: "x", port: 80 };
|
||||
cfg.host = "y"; // Error
|
||||
```
|
||||
|
||||
### Deep readonly
|
||||
```ts
|
||||
type DeepReadonly<T> = {
|
||||
readonly [K in keyof T]: T[K] extends object ? DeepReadonly<T[K]> : T[K];
|
||||
};
|
||||
|
||||
const data: DeepReadonly<{ user: { id: number } }> = { user: { id: 1 } };
|
||||
data.user.id = 2; // Error
|
||||
```
|
||||
|
||||
### `readonly T[]` in API
|
||||
```ts
|
||||
function sum(xs: readonly number[]): number {
|
||||
// xs.push(1); // Error
|
||||
return xs.reduce((a, b) => a + b, 0);
|
||||
}
|
||||
sum([1, 2, 3]); // OK
|
||||
sum(new Array(3).fill(0)); // OK
|
||||
```
|
||||
|
||||
### `as const` (literal narrowing)
|
||||
```ts
|
||||
const ROLES = ["admin", "user", "guest"] as const;
|
||||
type Role = typeof ROLES[number]; // "admin" | "user" | "guest"
|
||||
|
||||
const config = { host: "localhost", port: 80 } as const;
|
||||
// type: { readonly host: "localhost"; readonly port: 80 }
|
||||
```
|
||||
|
||||
### Tuple as const
|
||||
```ts
|
||||
function point() { return [1, 2] as const; } // [1, 2] 매 readonly tuple
|
||||
const [x, y] = point();
|
||||
```
|
||||
|
||||
### Combine with branded type
|
||||
```ts
|
||||
type ImmutableUser = Readonly<{ id: string & { __brand: "UserId" }; email: string }>;
|
||||
```
|
||||
|
||||
### Runtime + type (Object.freeze + as const)
|
||||
```ts
|
||||
function deepFreeze<T>(o: T): Readonly<T> {
|
||||
Object.freeze(o);
|
||||
for (const k of Object.keys(o as object) as (keyof T)[]) {
|
||||
const v = o[k];
|
||||
if (v && typeof v === "object" && !Object.isFrozen(v)) deepFreeze(v);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
const FROZEN = deepFreeze({ a: { b: 1 } });
|
||||
```
|
||||
|
||||
## 매 결정 기준
|
||||
| 상황 | Approach |
|
||||
|---|---|
|
||||
| Public function param | `readonly T[]` |
|
||||
| Config / constants | `as const` |
|
||||
| Class field 매 once-set | `readonly` modifier |
|
||||
| Runtime guarantee 필요 | `Object.freeze` (or Immer) |
|
||||
| Deep immutability type | Custom `DeepReadonly<T>` |
|
||||
|
||||
**기본값**: 매 input param 매 `readonly`, 매 constants 매 `as const`. 매 runtime needed 시 Immer.
|
||||
|
||||
## 🔗 Graph
|
||||
- 부모: [[TypeScript]] · [[Immutability]]
|
||||
- 응용: [[Redux Toolkit]] · [[Zustand]] · [[Type-Level Programming]]
|
||||
- Adjacent: [[Branded Types]] · [[Discriminated_Unions|Discriminated Unions]] · [[Variance]] · [[Mapped Types]]
|
||||
|
||||
## 🤖 LLM 활용
|
||||
**언제**: API design (input param immutability), config typing, state management types.
|
||||
**언제 X**: Runtime tamper-proof 필요 (use freeze), JS-only project.
|
||||
|
||||
## ❌ 안티패턴
|
||||
- **`readonly` 가 deep 일 거라 가정**: 매 shallow only — `DeepReadonly` 사용.
|
||||
- **`as` cast 으로 우회**: 매 escape hatch — type system 의 매 무용.
|
||||
- **`Readonly<T>` + `ReadonlyArray<T>` 혼동**: object vs array, Mapped vs interface.
|
||||
- **Mutating returned `readonly` array (via `as`)**: 매 compile pass 의 매 runtime corruption.
|
||||
- **Class field `readonly` 의 매 setter 통한 mutation**: 매 readonly 가 매 init time 만.
|
||||
- **Public API 매 `T[]` (mutable)**: 매 caller 의 의 mutation 가능 — 항상 `readonly T[]` 권장.
|
||||
|
||||
## 🧪 검증 / 중복
|
||||
- Verified (TypeScript handbook, TC39 records & tuples proposal, MS TypeScript blog).
|
||||
- 신뢰도 A.
|
||||
|
||||
## 🕓 Changelog
|
||||
| 날짜 | 변경 |
|
||||
|---|---|
|
||||
| 2026-05-08 | Phase 1 |
|
||||
| 2026-05-10 | Manual cleanup — readonly forms, as const, DeepReadonly pattern |
|
||||
Reference in New Issue
Block a user