--- id: wiki-2026-0508-ts-brand title: ts-brand category: 10_Wiki/Topics status: verified canonical_id: self aliases: [ts-brand library, Branded Types library, Nominal Typing TS] duplicate_of: none source_trust_level: A confidence_score: 0.9 verification_status: applied tags: [typescript, branded-types, nominal-typing, library, type-safety] raw_sources: [] last_reinforced: 2026-05-10 github_commit: pending tech_stack: language: typescript framework: 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`: 매 branded type 의 create. - `make()`: 매 cast helper. - 매 alternatives: 매 own-rolling, Effect-TS Brand, type-fest Opaque. ## 💻 패턴 ### 1. Basic Brand ```typescript import { Brand, make } from 'ts-brand'; type UserId = Brand; type OrderId = Brand; const UserId = make(); const OrderId = make(); 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) ```typescript type Email = Brand; 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) ```typescript import { z } from 'zod'; import type { Brand } from 'ts-brand'; type UserId = Brand; const UserIdSchema = z.string().uuid().brand<'UserId'>(); // ^? z.ZodBranded const id = UserIdSchema.parse('xxx-uuid'); // type: string & z.BRAND<'UserId'> // 매 Zod-native brand 의 ts-brand 와 의 compatible. ``` ### 4. Brand removal (rare, escape hatch) ```typescript function brandOf>(b: T): UnwrapBrand { return b as any; } type UnwrapBrand = T extends Brand ? U : never; const raw: string = brandOf(u); // 매 e.g., logging ``` ### 5. Multi-brand (intersection) ```typescript type NonEmpty = Brand; type Trimmed = Brand; 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|Nominal Typing]] - 변형: [[Branded Types]] · [[Opaque Types (TypeScript)]] - 응용: [[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 |