---
id: wiki-2026-0508-exhaustiveness-checking
title: Exhaustiveness Checking
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [exhaustive match, never type, sealed types, tagged union, switch exhaustive]
duplicate_of: none
source_trust_level: A
confidence_score: 0.95
verification_status: applied
tags: [type-system, typescript, rust, exhaustive, never-type, discriminated-union]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
language: TypeScript / Rust / Kotlin
---
# Exhaustiveness Checking
## 매 한 줄
> **"매 type-level 의 매 case 의 covered 의 verify"**. 매 switch / match 의 missing branch 의 compile-time 의 catch. 매 TypeScript `never`, 매 Rust `match`, 매 Kotlin sealed. 매 modern: 매 type-driven design 의 핵심.
## 매 핵심
### 매 mechanism
- **Discriminated union** + 매 switch.
- **Sealed types**: 매 known set.
- **never type** (TS): 매 unreachable.
- **Pattern match** (Rust, Scala, Kotlin).
### 매 응용
1. **State machine**: 매 모든 state 의 handle.
2. **Event handler**: 매 모든 event.
3. **Error type**: 매 union of errors.
4. **API response**.
5. **Theme / variant**.
## 💻 패턴
### TypeScript (never)
```typescript
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; // 매 compile error if Shape grows
throw new Error(`Unhandled: ${_exhaustive}`);
}
}
}
```
### assertNever helper
```typescript
function assertNever(x: never): never {
throw new Error(`Unexpected: ${JSON.stringify(x)}`);
}
function handle(s: Shape) {
switch (s.kind) {
case 'circle': return ...;
case 'square': return ...;
case 'rectangle': return ...;
default: return assertNever(s);
}
}
```
### Rust (match)
```rust
enum Shape {
Circle { radius: f64 },
Square { side: f64 },
Rectangle { w: f64, h: f64 },
}
fn area(s: &Shape) -> f64 {
match s {
Shape::Circle { radius } => std::f64::consts::PI * radius * radius,
Shape::Square { side } => side * side,
Shape::Rectangle { w, h } => w * h,
// 매 missing arm = compile error
}
}
```
### Kotlin sealed class
```kotlin
sealed class Shape {
data class Circle(val r: Double) : Shape()
data class Square(val side: Double) : Shape()
}
fun area(s: Shape) = when (s) {
is Shape.Circle -> Math.PI * s.r * s.r
is Shape.Square -> s.side * s.side
// 매 exhaustive — 매 compiler enforces
}
```
### State machine (TS)
```typescript
type AppState =
| { status: 'idle' }
| { status: 'loading'; startedAt: Date }
| { status: 'success'; data: Data }
| { status: 'error'; message: string };
function render(s: AppState) {
switch (s.status) {
case 'idle': return ;
case 'loading': return ;
case 'success': return ;
case 'error': return ;
default: return assertNever(s);
}
}
```
### Result type
```typescript
type Result = { tag: 'ok'; value: T } | { tag: 'err'; error: E };
function handle(r: Result): T {
switch (r.tag) {
case 'ok': return r.value;
case 'err': throw r.error;
default: return assertNever(r);
}
}
```
### Reducer (Redux)
```typescript
type Action =
| { type: 'add'; value: number }
| { type: 'sub'; value: number }
| { type: 'reset' };
function reducer(state: number, action: Action): number {
switch (action.type) {
case 'add': return state + action.value;
case 'sub': return state - action.value;
case 'reset': return 0;
default: return assertNever(action);
}
}
```
### Type predicate (narrow)
```typescript
function isCircle(s: Shape): s is Extract {
return s.kind === 'circle';
}
```
### TS 4.4+ template literal (string union)
```typescript
type HttpStatus = 200 | 404 | 500;
function describe(s: HttpStatus): string {
switch (s) {
case 200: return 'OK';
case 404: return 'Not Found';
case 500: return 'Server Error';
default: return assertNever(s);
}
}
```
### Compile fail demo
```typescript
type Theme = 'light' | 'dark' | 'sepia';
function bg(t: Theme): string {
switch (t) {
case 'light': return '#fff';
case 'dark': return '#000';
// 매 missing 'sepia'
default: const _: never = t; // ❌ Type 'sepia' is not assignable to 'never'
return '';
}
}
```
### ESLint rule
```javascript
// .eslintrc
{
rules: {
'@typescript-eslint/switch-exhaustiveness-check': 'error',
}
}
```
### Rust if-let / match guard
```rust
match (state, action) {
(State::Idle, Action::Start) => State::Running,
(State::Running, Action::Stop) => State::Idle,
(s, _) => s, // 매 catchall — 매 explicit 의 careful
}
```
## 매 결정 기준
| 상황 | Approach |
|---|---|
| TS state machine | Discriminated union + never |
| Rust enum | match (built-in) |
| Kotlin | sealed + when |
| Redux | tagged action + assertNever |
| API variant | Discriminated union |
| Throwaway script | Skip |
**기본값**: 매 TS = discriminated union + assertNever + ESLint rule. 매 Rust = match. 매 Kotlin = sealed when.
## 🔗 Graph
- 부모: [[TypeScript 타입 시스템 (TypeScript Type System)|Type-System]] · [[Static-Analysis]]
- 변형: [[Discriminated-Union]] · [[Sealed-Class]] · [[Pattern-Matching]]
- 응용: [[State-Machine]] · [[Result-Type]]
- Adjacent: [[Encapsulation-of-Domain-Invariants]] · [[Type-Driven-Design]] · [[Never-Type]]
## 🤖 LLM 활용
**언제**: 매 type-rich domain. 매 state machine. 매 reducer.
**언제 X**: 매 dynamic / heterogeneous.
## ❌ 안티패턴
- **Catchall default**: 매 exhaustiveness 의 lose.
- **String enum 의 boolean check**: 매 brittle.
- **No assertNever**: 매 default 의 silent.
- **Mix kind tag**: 매 narrow 의 fail.
- **Lint disabled**: 매 enforce X.
## 🧪 검증 / 중복
- Verified (TS handbook, Rust book, Effective TypeScript).
- 신뢰도 A.
## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — never + 매 TS / Rust / Kotlin / Redux exhaustive code |