Files
2nd/10_Wiki/Topics/Domain_Programming/Architecture/Discriminated_Unions.md
T
Antigravity Agent c24165b8bc refactor(topics): 멀티 에이전트용 지식 재편 — _Common(공통 기본기) + Domain_* 구조
에이전트 8종(대화형/프로그래머 C·S/디자이너/설계자/기획자/QA/PD/PM)에게
[공통 기본 능력 + 롤별 Specialty] 2층으로 지식을 주입하기 위한 재분류.
문서 내용·포맷은 무수정, 폴더 이동만 (6,372개 문서 수 보존 확인).

- Topic_Programming → Domain_Programming (내부 구조 보존)
- Topic_Graphic → Domain_Design
- Topic_Business → Domain_Product
- Topic_General → Domain_General
- _Common 신설: Math(구 Topic_Math_Specialty), Reasoning(구 General/From_Thinking & Reasoning),
  Reasoning_Creativity(구 General/From_창의성), Communication(Poetic_Blog_Writing + From_writing)
- 타 도메인의 From_* 폴더는 유지 (출처 표기일 뿐, 이미 도메인에 맞게 분류된 문서)
- 빈 폴더 정리 (memory/procedures)
- 에이전트→폴더 매핑은 workspace의 .astra/agent-knowledge-map.json (9개 에이전트)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-11 11:05:56 +09:00

6.2 KiB

id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, tags, raw_sources, last_reinforced, github_commit, tech_stack
id title category status canonical_id aliases duplicate_of source_trust_level confidence_score verification_status tags raw_sources last_reinforced github_commit tech_stack
wiki-2026-0508-discriminated-unions Discriminated Unions 10_Wiki/Topics verified self
Tagged Unions
Sum Types
Algebraic Data Types
ADT
none A 0.95 applied
typescript
types
functional
type-narrowing
2026-05-10 pending
language framework
typescript type-system

Discriminated Unions

매 한 줄

"매 한 literal field 의 매 type narrow". 매 TS / F# / Rust enum / Haskell ADT 의 same idea — 매 union 의 each variant 의 unique discriminant (tag) 의 carry, 매 compiler 의 매 switch 시 매 narrow. 매 2026 TS 5.7 의 매 dominant data modeling pattern.

매 핵심

매 anatomy

type Result<T, E> =
  | { kind: "ok"; value: T }
  | { kind: "err"; error: E };
//    ^^^^^^^^^^^ 매 discriminant — 매 string / number / boolean literal.

매 narrowing rules

  • 매 discriminant 의 literal 의 must.
  • switch / if 매 변수 의 narrow.
  • 매 exhaustive check 의 never 의 leverage.

매 vs alternatives

  • vs class hierarchy: 매 closed set, 매 pattern-match easy, 매 no instanceof.
  • vs enum: 매 enum 의 variant 별 data 의 X — DU 의 carry.
  • vs untagged union: 매 narrow 의 hard, 매 runtime check 의 brittle.

매 응용

  1. Result / Option / Either monads.
  2. State machine state.
  3. Redux / xstate action / event.
  4. API response variants.
  5. AST node types.

💻 패턴

Pattern 1: Result / Either

type Result<T, E = Error> =
  | { ok: true; value: T }
  | { ok: false; error: E };

function parseJson<T>(s: string): Result<T> {
  try { return { ok: true, value: JSON.parse(s) }; }
  catch (e) { return { ok: false, error: e as Error }; }
}

const r = parseJson<User>(input);
if (r.ok) {
  r.value.name; // 매 narrowed 의 T
} else {
  r.error.message; // 매 narrowed 의 E
}

Pattern 2: Exhaustive switch with never

type Shape =
  | { kind: "circle"; radius: number }
  | { kind: "square"; side: number }
  | { kind: "rectangle"; w: number; h: number };

function area(s: Shape): number {
  switch (s.kind) {
    case "circle": return Math.PI * s.radius ** 2;
    case "square": return s.side ** 2;
    case "rectangle": return s.w * s.h;
    default:
      const _exhaustive: never = s;  // 매 새 variant 추가 시 compile error
      throw new Error(`unhandled: ${_exhaustive}`);
  }
}

Pattern 3: State machine state

type FetchState<T> =
  | { status: "idle" }
  | { status: "loading"; abortCtrl: AbortController }
  | { status: "success"; data: T; fetchedAt: Date }
  | { status: "error"; error: Error; retryCount: number };

function reducer<T>(s: FetchState<T>, ev: FetchEvent<T>): FetchState<T> {
  if (s.status === "loading" && ev.type === "abort") {
    s.abortCtrl.abort();
    return { status: "idle" };
  }
  // ...
}

Pattern 4: Redux-style action

type TodoAction =
  | { type: "ADD"; text: string }
  | { type: "TOGGLE"; id: string }
  | { type: "DELETE"; id: string }
  | { type: "EDIT"; id: string; text: string };

function reducer(state: Todo[], action: TodoAction): Todo[] {
  switch (action.type) {
    case "ADD": return [...state, { id: uid(), text: action.text, done: false }];
    case "TOGGLE": return state.map(t => t.id === action.id ? { ...t, done: !t.done } : t);
    case "DELETE": return state.filter(t => t.id !== action.id);
    case "EDIT": return state.map(t => t.id === action.id ? { ...t, text: action.text } : t);
  }
}

Pattern 5: API response variants

type ApiResponse<T> =
  | { status: 200; data: T }
  | { status: 401; reason: "expired" | "invalid" }
  | { status: 429; retryAfterSec: number }
  | { status: 500; traceId: string };

async function call<T>(url: string): Promise<ApiResponse<T>> { /* ... */ }

Pattern 6: Pattern-match helper (ts-pattern)

import { match, P } from "ts-pattern";

const message = match(response)
  .with({ status: 200 }, r => `Got ${r.data.length} items`)
  .with({ status: 401, reason: "expired" }, () => "Please refresh token")
  .with({ status: 429 }, r => `Wait ${r.retryAfterSec}s`)
  .with({ status: 500 }, r => `Error ${r.traceId}`)
  .exhaustive();

Pattern 7: Zod parsing → DU

import { z } from "zod";

const Event = z.discriminatedUnion("type", [
  z.object({ type: z.literal("click"), x: z.number(), y: z.number() }),
  z.object({ type: z.literal("key"), key: z.string() }),
  z.object({ type: z.literal("scroll"), dy: z.number() }),
]);
type Event = z.infer<typeof Event>;

Pattern 8: assertNever helper

export function assertNever(x: never): never {
  throw new Error(`unexpected: ${JSON.stringify(x)}`);
}
// 매 default case 의 use.

매 결정 기준

상황 Approach
Closed set of variants DU
Open extensible class + interface
Boolean flag pair DU (truthy combos 의 prevent)
Many variants (~20+) DU + ts-pattern
Server boundary Zod discriminatedUnion

기본값: 매 union 의 require 시 매 DU + kind / type / status discriminant.

🔗 Graph

🤖 LLM 활용

언제: 매 type modeling, 매 state machine, 매 API contract, 매 reducer. 언제 X: 매 single-variant — 매 plain interface.

안티패턴

  • No discriminant: 매 untagged union — 매 narrow 의 hard.
  • String discriminant 의 typo: 매 magic string 의 const 의 hoist.
  • Boolean flag combos: {loading, success, error} boolean — 매 DU 의 use.
  • Default case 의 swallow: default: return state — 매 새 variant 시 silent miss.
  • Class hierarchy 의 simulate: 매 DU 의 cleaner.

🧪 검증 / 중복

  • Verified (TS handbook "Narrowing", Rust enum docs, F# DU, Zod docs, ts-pattern docs).
  • 신뢰도 A.

🕓 Changelog

날짜 변경
2026-05-08 Phase 1
2026-05-10 Manual cleanup — DU patterns + exhaustive + ts-pattern + Zod