type-safety-patterns

Implement TypeScript type guards, generics, branded types, and discriminated unions.

2|1|Updated Nov 13, 2025
One-click install
npx skills add https://github.com/MolcajeteAI/plugin --skill type-safety-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: type-safety-patterns
Source: https://github.com/MolcajeteAI/plugin/tree/main/deprecated/tech-stacks/js/common/skills/type-safety-patterns
Command: npx skills add https://github.com/MolcajeteAI/plugin --skill type-safety-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps developers build more reliable and maintainable TypeScript code by leveraging advanced type system features to catch errors at compile time and improve code clarity.

Core Features & Use Cases

  • Type Guards: Implement runtime checks for complex types, ensuring data integrity.
  • Generics: Create reusable, type-safe functions and classes that work with various data types.
  • Branded Types: Prevent accidental mixing of types that have the same underlying representation (e.g., UserId vs. OrderId).
  • Discriminated Unions: Model state machines and API responses with clear, type-safe variants.
  • Use Case: When designing a public API, use branded types for all identifiers and discriminated unions for API responses to ensure consumers use the types correctly and safely.

Quick Start

Use the type-safety-patterns skill to generate an example of a user-defined type guard in TypeScript.

Frequently Asked Questions about type-safety-patterns

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

FAQPage Schema
How do I use type guards in TypeScript to check unknown data at runtime?

Type guards in TypeScript enable runtime checks for complex types, ensuring data integrity when handling unknown inputs. You can use user-defined type guards to narrow down types and validate external data structures dynamically.

What are branded types in TypeScript and when do I need them?

Branded types in TypeScript prevent accidental mixing of types sharing the same underlying representation, such as UserId and OrderId. You need them when designing public APIs to ensure consumers use identifiers correctly and safely.

How do I use discriminated unions to model API responses in TypeScript?

Discriminated unions model API responses with clear, type-safe variants in TypeScript. They allow you to define state machines and handle different response states by checking a shared discriminant property at compile time.

What's the best way to create reusable TypeScript functions with generics?

Generics are the best way to create reusable, type-safe functions and classes in TypeScript that work with various data types. They allow you to write flexible utilities while maintaining strict compile-time error prevention.

Can I prevent compile-time errors in TypeScript when designing type-safe APIs?

You can prevent compile-time errors in TypeScript by leveraging advanced type system features like branded types, discriminated unions, and generics. Adhering to strict type definitions ensures robust API design and early error detection.