223 lines
7.1 KiB
Markdown
223 lines
7.1 KiB
Markdown
---
|
|
id: wiki-2026-0508-never-타입
|
|
title: never 타입
|
|
category: 10_Wiki/Topics
|
|
status: verified
|
|
canonical_id: self
|
|
aliases: [never, never type, TypeScript never, bottom type]
|
|
duplicate_of: none
|
|
source_trust_level: A
|
|
confidence_score: 0.9
|
|
verification_status: applied
|
|
tags: [typescript, never, exhaustiveness, type-system, bottom-type]
|
|
raw_sources: []
|
|
last_reinforced: 2026-05-10
|
|
github_commit: pending
|
|
tech_stack:
|
|
language: typescript
|
|
framework: typescript-5
|
|
---
|
|
|
|
# never 타입
|
|
|
|
## 매 한 줄
|
|
> **"매 TypeScript 의 bottom type — 매 absurd, 매 도달 불가능한 value 의 type"**. 매 throw 함수 / infinite loop / exhaustive switch 의 missed case 표현. 매 `never` 매 모든 type 의 subtype, 매 union 에서 absorbed, 매 conditional type 의 filter. 매 type-driven design 의 핵심.
|
|
|
|
## 매 핵심
|
|
|
|
### 매 정체
|
|
- **Bottom type**: 매 모든 type 의 subtype — 매 `never extends T` 매 항상 true.
|
|
- **Empty set**: 매 어떤 value 도 가질 수 없음 — 매 `const x: never = ...` 매 항상 error.
|
|
- **Union absorption**: 매 `T | never` = `T`. 매 union 에서 사라짐.
|
|
- **Intersection 의 sink**: 매 `T & never` = `never`. 매 incompatible intersection 의 결과.
|
|
|
|
### 매 발생 case
|
|
- 매 함수 의 `throw` 만: `function fail(): never { throw Error(); }`.
|
|
- 매 infinite loop: `function loop(): never { while (true) {} }`.
|
|
- 매 narrow 후 impossible: `if (typeof x === 'string' && typeof x === 'number')` 안 의 `x` 매 `never`.
|
|
- 매 exhaustive switch 의 default: 매 모든 case handle 후 default 의 `_: never`.
|
|
|
|
### 매 응용
|
|
1. **Exhaustiveness check**: switch 의 모든 case 처리 강제.
|
|
2. **Conditional type filter**: `T extends X ? T : never` — narrow union.
|
|
3. **Impossible state**: 매 type 으로 state 불가능 표현.
|
|
4. **Function overload sentinel**: 매 reachable 못한 overload 의 표시.
|
|
5. **Branded type uniqueness**: `__brand: never` field.
|
|
|
|
## 💻 패턴
|
|
|
|
### Exhaustive switch
|
|
```ts
|
|
type Shape =
|
|
| { kind: 'circle'; radius: number }
|
|
| { kind: 'square'; size: number }
|
|
| { kind: 'triangle'; base: number; height: number };
|
|
|
|
function area(s: Shape): number {
|
|
switch (s.kind) {
|
|
case 'circle': return Math.PI * s.radius ** 2;
|
|
case 'square': return s.size ** 2;
|
|
case 'triangle': return 0.5 * s.base * s.height;
|
|
default:
|
|
const _exhaustive: never = s;
|
|
// 매 새 shape 추가 시 여기서 type error → 모든 case handle 강제
|
|
throw new Error(`Unhandled: ${_exhaustive}`);
|
|
}
|
|
}
|
|
```
|
|
|
|
### Conditional type filter
|
|
```ts
|
|
type NonNullable<T> = T extends null | undefined ? never : T;
|
|
|
|
type Result = NonNullable<string | null | undefined>;
|
|
// 매 string
|
|
|
|
type Extract<T, U> = T extends U ? T : never;
|
|
type StringOnly = Extract<string | number | boolean, string>;
|
|
// 매 string
|
|
```
|
|
|
|
### assertNever utility
|
|
```ts
|
|
function assertNever(x: never): never {
|
|
throw new Error(`Unexpected: ${JSON.stringify(x)}`);
|
|
}
|
|
|
|
function handleEvent(e: 'click' | 'hover' | 'focus') {
|
|
switch (e) {
|
|
case 'click': return ...;
|
|
case 'hover': return ...;
|
|
case 'focus': return ...;
|
|
default: return assertNever(e); // 매 type-safe + runtime safety
|
|
}
|
|
}
|
|
```
|
|
|
|
### Impossible state via never
|
|
```ts
|
|
type LoadingState =
|
|
| { status: 'idle'; data: never; error: never }
|
|
| { status: 'loading'; data: never; error: never }
|
|
| { status: 'success'; data: User; error: never }
|
|
| { status: 'error'; data: never; error: string };
|
|
|
|
function render(s: LoadingState) {
|
|
if (s.status === 'success') s.data; // 매 User
|
|
if (s.status === 'error') s.error; // 매 string
|
|
// 매 never field 매 access 시 type error
|
|
}
|
|
```
|
|
|
|
### Throw helper
|
|
```ts
|
|
function unreachable(msg: string): never {
|
|
throw new Error(`Unreachable: ${msg}`);
|
|
}
|
|
|
|
function divide(a: number, b: number): number {
|
|
if (b === 0) return unreachable('division by zero');
|
|
return a / b;
|
|
// 매 unreachable 매 never 반환 → narrow 가능
|
|
}
|
|
```
|
|
|
|
### Branded type — uniqueness via never
|
|
```ts
|
|
type Brand<T, B> = T & { readonly __brand: B };
|
|
type UserId = Brand<string, 'UserId'>;
|
|
type PostId = Brand<string, 'PostId'>;
|
|
|
|
const u: UserId = 'abc' as UserId;
|
|
const p: PostId = u; // 매 type error — different brands
|
|
// 매 __brand 가 never 도 가능: type Brand<T, B> = T & { readonly __brand: never };
|
|
// (단 distinct brand 위해 phantom 필요)
|
|
```
|
|
|
|
### Distributive conditional with never
|
|
```ts
|
|
type Filter<T, U> = T extends U ? T : never;
|
|
|
|
type Foo = Filter<'a' | 'b' | 1 | 2, string>;
|
|
// 매 'a' | 'b' — 매 distributive 하게 each variant 검사,
|
|
// 매 number variant 매 never 로 배제.
|
|
```
|
|
|
|
### Function overload — never as sentinel
|
|
```ts
|
|
function fetchUser(): Promise<User>;
|
|
function fetchUser(id: string): Promise<User | null>;
|
|
function fetchUser(id?: string): Promise<User | null> {
|
|
if (id === undefined) return getCurrentUser();
|
|
return getUserById(id);
|
|
}
|
|
|
|
// 매 overload mismatch 시 implementation 의 parameter type 매 never 로 narrow.
|
|
```
|
|
|
|
### Mapped type — filter keys
|
|
```ts
|
|
type FunctionKeys<T> = {
|
|
[K in keyof T]: T[K] extends Function ? K : never;
|
|
}[keyof T];
|
|
|
|
class Foo {
|
|
x: number = 0;
|
|
y: string = '';
|
|
greet() {}
|
|
fly() {}
|
|
}
|
|
|
|
type Methods = FunctionKeys<Foo>; // 매 'greet' | 'fly'
|
|
```
|
|
|
|
### Empty array literal type
|
|
```ts
|
|
const empty: never[] = [];
|
|
empty.push(1); // 매 type error — never 에 number 할당 X
|
|
|
|
// 매 generic constraint
|
|
function head<T>(arr: readonly T[]): T | never {
|
|
if (arr.length === 0) throw new Error();
|
|
return arr[0];
|
|
}
|
|
```
|
|
|
|
## 매 결정 기준
|
|
| 상황 | Approach |
|
|
|---|---|
|
|
| Switch exhaustive | `default: const _: never = x`. |
|
|
| Throw helper return type | `: never`. |
|
|
| Conditional filter | `T extends U ? T : never`. |
|
|
| Impossible field | `field: never` in union member. |
|
|
| Generic constraint impossible | `T extends never` (rarely useful). |
|
|
|
|
**기본값**: 매 exhaustive check 매 `assertNever(x)`. 매 conditional type 의 filter 매 `never` branch.
|
|
|
|
## 🔗 Graph
|
|
- 부모: [[TypeScript]] · [[Type System]]
|
|
- 변형: [[unknown]] · [[void]] · [[any]]
|
|
- 응용: [[Exhaustive Checking]] · [[Discriminated Unions]] · [[Branded Types for Nominal Typing]]
|
|
- Adjacent: [[Conditional Types]] · [[Mapped Types]] · [[as const]]
|
|
|
|
## 🤖 LLM 활용
|
|
**언제**: 매 exhaustive switch enforcement, throw helper, conditional type filter, impossible state encoding 의.
|
|
**언제 X**: 매 simple optional (`T | undefined`) — never 매 overkill, 매 runtime null check 가 더 명확한 case.
|
|
|
|
## ❌ 안티패턴
|
|
- **`x: never` 의 value 할당 시도**: 매 `const x: never = 1` 매 항상 error. 매 type-only.
|
|
- **never[] 의 push**: 매 element 추가 X — 매 generic 으로 풀어야.
|
|
- **catch (e) 매 never 가정**: 매 TS 4.4+ 의 catch 매 default unknown — never 아님.
|
|
- **Promise<never>**: 매 reject-only Promise 의 type 으로 의도 — 매 사실은 `Promise<unknown>` 또는 throw helper 가 더 명확.
|
|
- **assertNever 없는 default**: 매 switch default 가 그냥 `return` — 매 case 추가 시 silent miss.
|
|
|
|
## 🧪 검증 / 중복
|
|
- Verified (TypeScript Handbook, TS deep dive, Anders Hejlsberg talks).
|
|
- 신뢰도 A.
|
|
|
|
## 🕓 Changelog
|
|
| 날짜 | 변경 |
|
|
|---|---|
|
|
| 2026-05-08 | Phase 1 |
|
|
| 2026-05-10 | Manual cleanup — never type patterns, exhaustive check, conditional filter, branded types 추가 |
|