typescript-advanced-patterns

Implement advanced TypeScript patterns for type-safe APIs and domain models.

4|1|Updated Aug 15, 2024
One-click install
npx skills add https://github.com/blockmatic-icebox/basilic-old --skill typescript-advanced-patterns-blockmatic-icebox
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: typescript-advanced-patterns
Source: https://github.com/blockmatic-icebox/basilic-old/tree/main/.cursor/skills/typescript-v5
Command: npx skills add https://github.com/blockmatic-icebox/basilic-old --skill typescript-advanced-patterns-blockmatic-icebox

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

TypeScript's advanced type system can become difficult to wield safely at scale; this skill clarifies and codifies patterns that enable strong type-safety, maintainability, and predictable APIs in complex domains.

Core Features & Use Cases

  • Use conditional types for precise type selection based on runtime-like conditions.
  • Leverage mapped and branded types to compose safe object shapes and prevent primitive mixing.
  • Implement discriminated unions and type guards to model exhaustive states and robust runtime checks.
  • Integrate with runtime validators (e.g., zod) for end-to-end type safety across boundaries.
  • Real-world use cases include building type-safe API clients, domain models, and library APIs with clear compile-time guarantees.

Quick Start

Apply the patterns to model a small domain with type-safe APIs and compile-time guarantees.

Frequently Asked Questions about typescript-advanced-patterns

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

FAQPage Schema
How do I use TypeScript discriminated unions to model exhaustive states?

TypeScript branded types prevent primitive mixing by attaching a unique type tag to a base primitive at compile time. This allows you to enforce strict domain constraints, such as distinguishing between a UserId and an Email string, without adding runtime overhead.

What's the best way to replace any with unknown for safe type inference?

Mapped types in TypeScript compose safe object shapes by iterating over an existing set of keys to apply transformations, such as making properties readonly or optional. This mechanism provides precise compile-time guarantees for complex domain models and library APIs.

Can I integrate TypeScript type guards with runtime validators like zod?

Conditional types in TypeScript enable precise type selection by evaluating type relationships at compile time, resembling runtime conditional logic. They are essential for building type-safe API clients and complex domain modeling where inferred types depend on specific input properties.

When should I use branded types to prevent primitive mixing in domain modeling?

TypeScript branded types prevent primitive mixing by attaching a unique type tag to a base primitive at compile time. This allows you to enforce strict domain constraints, such as distinguishing between a UserId and an Email string, without adding runtime overhead.

How do mapped types work for composing safe object shapes in TypeScript?

Mapped types in TypeScript compose safe object shapes by iterating over an existing set of keys to apply transformations, such as making properties readonly or optional. This mechanism provides precise compile-time guarantees for complex domain models and library APIs.

Does integrating zod with TypeScript provide end-to-end type safety across boundaries?

Integrating TypeScript type guards with runtime validators like zod enables end-to-end type safety across system boundaries by verifying external input at runtime. This approach ensures the parsed runtime data precisely matches the static compile-time domain types.