branded-types

Create type-safe distinct primitives in TypeScript using phantom branding.

35|4|Updated Dec 18, 2024
One-click install
npx skills add https://github.com/wellcrafted-dev/wellcrafted --skill branded-types
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: branded-types
Source: https://github.com/wellcrafted-dev/wellcrafted/tree/main/skills/branded-types
Command: npx skills add https://github.com/wellcrafted-dev/wellcrafted --skill branded-types

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill solves the problem of TypeScript's structural typing allowing interchangeable primitive types (like strings or numbers) that should be distinct, preventing accidental misuse of values like user IDs and order IDs.

Core Features & Use Cases

  • Nominal Typing: Creates distinct types from primitives using phantom branding.
  • Type Safety: Prevents mixing up different kinds of IDs, tokens, or other primitives at compile time.
  • Brand Constructor Pattern: Encourages a single source of truth for creating branded types, optionally including runtime validation.
  • Use Case: Define UserId and OrderId as distinct types, ensuring that a function expecting a UserId cannot accidentally receive an OrderId.

Quick Start

Use the branded-types skill to create a new type ProductId that is a string and cannot be confused with other strings.

Frequently Asked Questions about branded-types

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I prevent TypeScript from treating different string IDs as the same type?

To prevent TypeScript from treating different string IDs as the same type, you can use branded types to implement nominal typing, creating distinct types like UserId and OrderId that cannot be accidentally interchanged at compile time.

What is the brand constructor pattern in TypeScript?

The brand constructor pattern in TypeScript provides a centralized source of truth for creating branded types, allowing you to define distinct primitives and optionally include runtime validation to ensure type safety and data integrity.

How do I create nominal types in TypeScript for primitive values?

You create nominal types in TypeScript by applying phantom branding to primitive values, which structurally separates identical primitives like strings or numbers so they cannot be mistakenly assigned to one another.

When do I need to use branded types in TypeScript?

You need to use branded types in TypeScript when structural typing causes accidental interchangeability of similar primitives, such as mixing up different kinds of IDs or tokens in function parameters and variable assignments.

Can I add runtime validation when creating distinct primitive types?

Yes, you can add runtime validation when creating distinct primitive types by utilizing the brand constructor pattern, which supports optional validation checks to verify data integrity alongside compile-time type safety.

Does TypeScript structural typing allow mixing different ID types?

Yes, TypeScript structural typing allows mixing different ID types because it compares shape rather than identity, but branded types solve this by adding phantom branding to enforce strict nominal typing and prevent accidental misuse.