using-generics

Apply generic constraints and mapped types in TypeScript code.

Updated Nov 21, 2025
One-click install
npx skills add https://github.com/djankies/claude-configs --skill using-generics
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: using-generics
Source: https://github.com/djankies/claude-configs/tree/main/typescript/skills/using-generics
Command: npx skills add https://github.com/djankies/claude-configs --skill using-generics

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Teaches generic constraints, avoiding any in generic defaults, and mapped types in TypeScript.

Core Features & Use Cases

  • Generic parameters & constraints: ensure type-safe API boundaries.
  • Defaults & mapped types: provide sensible defaults while preserving safety.
  • Use Case: create a reusable identity function that preserves input type.

Quick Start

Create a generic function with constraints and a mapped type example.

Frequently Asked Questions about using-generics

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

FAQPage Schema
How do I write reusable TypeScript functions that preserve type safety?

Use generics with constraints to write reusable functions while maintaining type safety. Constraints ensure the function receives only compatible types, preventing runtime errors and enabling IDE autocomplete. This approach works across frontend and library code.

What's the difference between generic constraints and default type parameters in TypeScript?

Generic constraints restrict what types a generic parameter can accept, enforcing safety at compile time. Avoid using `any` as a default; instead use mapped types to transform types safely while preserving specificity and preventing type-safety gaps.

How do mapped types help with generic type transformations?

Mapped types iterate over object properties to create new types dynamically, enabling robust type transformations without repetition. Combined with generics and constraints, they enforce type safety across reusable APIs and libraries.

Can I use generic constraints with classes and APIs?

Yes. Generic constraints work with classes, functions, and APIs by restricting type parameters at the API boundary. This ensures type-safe reusable classes and generic APIs that validate input types upfront.

Why should I avoid `any` in generic default values?

Using `any` as a generic default bypasses type safety and defeats the purpose of constraints. Instead, use mapped and conditional types to provide sensible defaults while maintaining strict type checking throughout your codebase.