--- id: P-REINFORCE-AI-DU-STATES category: "10_Wiki/πŸ’‘ Topics/AI" confidence_score: 0.99 tags: [TypeScript, StateManagement, Patterns, Architecture] last_reinforced: 2026-04-20 --- # [[Discriminated-Unions-for-State-Modeling]] (μƒνƒœ λͺ¨λΈλ§μ„ μœ„ν•œ κ΅¬λ³„λœ μœ λ‹ˆμ˜¨) ## πŸ“Œ ν•œ 쀄 톡찰 (The Karpathy Summary) > "λΆˆκ°€λŠ₯ν•œ μƒνƒœ(Impossible State)λ₯Ό μ½”λ“œ μˆ˜μ€€μ—μ„œ μ›μ²œ λ΄‰μ‡„ν•˜λΌ." λ‘œλ”© μ€‘μ΄λ©΄μ„œ λ™μ‹œμ— μ—λŸ¬κ°€ λ‚  수 μ—†λŠ” κ²ƒμ²˜λŸΌ, μ‹œμŠ€ν…œμ˜ μƒν˜Έ 배타적인 μƒνƒœλ“€μ„ νƒ€μž…μ„ 톡해 μ™„λ²½ν•˜κ²Œ μ •μ˜ν•˜λŠ” 기법이닀. ## πŸ“– κ΅¬μ‘°ν™”λœ 지식 (Synthesized Content) - **The Anti-pattern**: - `interface State { isLoading: boolean; error?: string; data?: Data; }` - 이 μ„€κ³„λŠ” `isLoading: true`μ΄λ©΄μ„œ λ™μ‹œμ— `error`κ°€ μ‘΄μž¬ν•˜λŠ” λͺ¨μˆœλœ μƒνƒœλ₯Ό ν—ˆμš©ν•œλ‹€. - **The Discriminated Union Solution**: - `type State = { type: 'loading' } | { type: 'error'; message: string } | { type: 'success'; data: Data };` - `type` 속성을 톡해 ν˜„μž¬ μ–΄λ–€ μƒνƒœμΈμ§€ λͺ…ν™•νžˆ κ΅¬λ³„ν•˜λ©°, 각 μƒνƒœμ— κΌ­ ν•„μš”ν•œ λ°μ΄ν„°λ§Œ κ°€μ§ˆ 수 있게 κ°•μ œν•œλ‹€. - **Benefit**: μ»΄ν¬λ„ŒνŠΈλ‚˜ λ‘œμ§μ—μ„œ 쑰건문 λΆ„κΈ°κ°€ 맀우 λͺ…ν™•ν•΄μ§€λ©°, λŸ°νƒ€μž„ μ—λŸ¬ λ°œμƒ κ°€λŠ₯성이 획기적으둜 쀄어든닀. ## ⚠️ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (RL Update) - μƒνƒœκ°€ λ³΅μž‘ν•΄μ§€λ©΄(예: 뢀뢄적 성곡, λ©€ν‹° μŠ€ν… 폼 λ“±) μœ λ‹ˆμ˜¨μ˜ 쑰합이 κΈ°ν•˜κΈ‰μˆ˜μ μœΌλ‘œ λŠ˜μ–΄λ‚  수 μžˆλ‹€. μ΄λ•ŒλŠ” μƒνƒœ λ¨Έμ‹ (State Machine, 예: XState) 라이브러리λ₯Ό λ„μž…ν•˜μ—¬ νƒ€μž… μ•ˆμ „μ„±κ³Ό λΉ„μ¦ˆλ‹ˆμŠ€ 흐름 μ œμ–΄λ₯Ό λ™μ‹œμ— μž‘λŠ” 것이 ꢌμž₯λœλ‹€. ## πŸ”— 지식 μ—°κ²° (Graph) - Related: [[Discriminated-Unions]] , [[Finite-State-Machines]] - Context: React-Query-Pattern