using-type-guards

Narrow unknown types at runtime with custom type predicates in TypeScript.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Teaches how to write custom type guards with type predicates and use built-in type narrowing in TypeScript.

Core Features & Use Cases

  • Custom type predicates: create conditions that narrow types.
  • Validation patterns: use type guards with runtime data.
  • Use Case: guard API responses before accessing properties.

Quick Start

Implement a simple isUser guard and use it to safely access user.name.

Frequently Asked Questions about using-type-guards

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

FAQPage Schema
How do I safely handle unknown types at runtime in TypeScript?

Type guards narrow unknown or any types at runtime, enabling safe property access. Use custom type predicates or built-in guards to validate data before accessing properties, protecting against undefined behavior when working with dynamic values from APIs or user input.

What's the best way to validate API responses in TypeScript?

Type guards with type predicates validate API responses before accessing properties. Create a custom guard function that checks the response structure, then use it in a conditional to narrow the type, ensuring type-safe access to validated data.

How do I create a custom type predicate in TypeScript?

Write a function with a return type of `is Type` that performs runtime checks on input data. The function returns true when the data matches the expected shape, and TypeScript narrows the type inside subsequent conditional blocks.

Can I use type guards with union types?

Yes. Type guards narrow union types by testing which member the value satisfies. After a guard check passes, TypeScript restricts the type to the matched member, eliminating type errors from accessing union-exclusive properties.

When should I use assertion functions instead of type guards?

Assertion functions throw on failure and narrow types for the rest of the scope, unlike type guards which use conditionals. Use assertions when invalid data should halt execution; use type guards when you need to branch logic based on the narrowed type.

Do I need external libraries to implement runtime validation with type guards?

No. Type guards and assertion functions are built-in TypeScript features requiring no dependencies. You write custom predicates using standard JavaScript logic, though external validation libraries offer pre-built guards for common patterns.