Files
2nd/10_Wiki/Topics/Programming & Language/ts-brand.md
T
Antigravity Agent f8b21af4be Wiki cleanup: error-doc removal, dedup merge, link normalization
10_Wiki/Topics 대규모 정리:
- 오류 캡처/미완성 stub 문서 227개 제거
- 교차폴더 중복 43클러스터 병합 (63파일 → redirect)
- 링크명 정규화: 깨진 링크 수정·redirect 직결·개념 매핑 ~2,400건
- 카테고리 MOC 6개 신규 생성
- Graph 섹션 미해결 related-keyword 링크 10,058건 제거

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-20 23:52:15 +09:00

4.4 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-ts-brand ts-brand 10_Wiki/Topics verified self
ts-brand library
Branded Types library
Nominal Typing TS
none A 0.9 applied
typescript
branded-types
nominal-typing
library
type-safety
2026-05-10 pending
language framework
typescript ts-brand

ts-brand

매 한 줄

"매 zero-runtime-cost 의 nominal typing 의 TypeScript". ts-brand 는 매 structural typing 의 default 의 TypeScript 에 매 nominal-style brand 의 추가 → 매 UserId 와 매 OrderId 가 매 둘 다 string 이지만 매 swap 의 compile error. 매 2026 의 standard pattern — 매 Effect-TS, Zod, neverthrow 의 모두 의 leverage.

매 핵심

매 problem (structural typing 의 limitation)

  • 매 TypeScript: 매 type UserId = string; type OrderId = string → 매 둘 다 동일 의 interchangeable.
  • 매 logical bug: 매 fn(userId: UserId) 의 orderId 의 pass 가 의 silent.

매 solution (Brand)

  • 매 phantom type tag 의 use → 매 compile-time discrimination.
  • 매 runtime cost = 0 (type-only).

매 ts-brand API

  • Brand<Base, Tag>: 매 branded type 의 create.
  • make<T>(): 매 cast helper.
  • 매 alternatives: 매 own-rolling, Effect-TS Brand, type-fest Opaque.

💻 패턴

1. Basic Brand

import { Brand, make } from 'ts-brand';

type UserId = Brand<string, 'UserId'>;
type OrderId = Brand<string, 'OrderId'>;

const UserId = make<UserId>();
const OrderId = make<OrderId>();

const u: UserId = UserId('u-123');
const o: OrderId = OrderId('o-456');

function fetchUser(id: UserId) { /* ... */ }
fetchUser(u);     // ✓
fetchUser(o);     // ✗ Type error
fetchUser('raw'); // ✗ Type error

2. Validated Brand (with runtime check)

type Email = Brand<string, 'Email'>;

function parseEmail(s: string): Email {
  if (!/^[^@]+@[^@]+\.[^@]+$/.test(s)) throw new Error(`invalid email: ${s}`);
  return s as Email;
}

// 매 user 의 input 의 parseEmail 의 통과 만 의 Email 의 acquire.
const e = parseEmail(req.body.email);

3. Brand + Zod (parse 시 brand)

import { z } from 'zod';
import type { Brand } from 'ts-brand';

type UserId = Brand<string, 'UserId'>;
const UserIdSchema = z.string().uuid().brand<'UserId'>();
//          ^? z.ZodBranded<z.ZodString, 'UserId'>

const id = UserIdSchema.parse('xxx-uuid'); // type: string & z.BRAND<'UserId'>
// 매 Zod-native brand 의 ts-brand 와 의 compatible.

4. Brand removal (rare, escape hatch)

function brandOf<T extends Brand<any, any>>(b: T): UnwrapBrand<T> {
  return b as any;
}
type UnwrapBrand<T> = T extends Brand<infer U, any> ? U : never;

const raw: string = brandOf(u); // 매 e.g., logging

5. Multi-brand (intersection)

type NonEmpty = Brand<string, 'NonEmpty'>;
type Trimmed = Brand<string, 'Trimmed'>;
type Clean = NonEmpty & Trimmed;

function clean(s: string): Clean {
  const t = s.trim();
  if (!t) throw new Error('empty');
  return t as Clean;
}

매 결정 기준

상황 Approach
매 ID type (UserId, OrderId) brand 의 always
매 validated string (Email, URL) brand + parse function
매 unit type (Meters, Seconds) brand 의 use
매 throwaway local 매 brand 의 skip
매 Zod ecosystem z.brand() 의 native 의 prefer

기본값: 매 domain 의 distinct identity 의 string/number type → 매 brand 의 use.

🔗 Graph

  • 부모: TypeScript · Nominal-Typing-in-TypeScript
  • 변형: Branded Types · Opaque Types
  • 응용: Zod · Effect-TS · Type Safety
  • Adjacent: Structural Typing · Runtime Validation

🤖 LLM 활용

언제: TypeScript domain modeling, ID type 의 distinguish, validation pipeline. 언제 X: 매 simple script, runtime-only language (Python, JS).

안티패턴

  • Cast 의 brand 의 bypass: 매 type safety 의 break.
  • Brand 의 너무 많음: 매 cognitive overhead.
  • Runtime check 없음 의 external input: 매 brand 만 의 false security.

🧪 검증 / 중복

  • Verified (ts-brand npm, Effect-TS Brand docs, Zod 4.x).
  • 신뢰도 A.

🕓 Changelog

날짜 변경
2026-05-08 Phase 1
2026-05-10 Manual cleanup — ts-brand 의 API, Zod integration, patterns 의 expand