Files
2nd/10_Wiki/Topic_Programming/Frontend/never 타입.md
T
Antigravity Agent 9148c358d0 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 폴더 제거.
2026-07-05 00:33:48 +09:00

7.1 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-never-타입 never 타입 10_Wiki/Topics verified self
never
never type
TypeScript never
bottom type
none A 0.9 applied
typescript
never
exhaustiveness
type-system
bottom-type
2026-05-10 pending
language framework
typescript 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') 안 의 xnever.
  • 매 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

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

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

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

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

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

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

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

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

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

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

🤖 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: 매 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 추가