avoiding-any-types

Replace TypeScript any types with unknown and type guards.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill teaches how to replace abusive uses of the any type with unknown and robust type guards, improving type safety across APIs and external data handling.

Core Features & Use Cases

  • Prefer known types, unions, and generics before resorting to any.
  • Use unknown with runtime validation to safely access data.
  • Implement type guards to narrow data safely.

Quick Start

Convert a function that accepts any input to use unknown and validate with a type guard before use.

Frequently Asked Questions about avoiding-any-types

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

FAQPage Schema
Why should I avoid using any in TypeScript?

The any type bypasses TypeScript's type checking, allowing runtime errors from unexpected data shapes. Replacing any with unknown and type guards catches type mismatches at compile time, preventing bugs from APIs, JSON, and user input before code executes.

How do I replace any with unknown and type guards?

Convert any parameters to unknown, then implement type guards to validate data before access. Use the pattern: accept unknown input, check its shape with a type guard function, narrow to a safe type, then access properties. This ensures runtime validation matches your type assumptions.

What's the difference between any and unknown in TypeScript?

Unknown is type-safe; it blocks all operations until you prove the data's shape through a type guard. Any disables all checks. Unknown forces explicit validation, making type errors visible during development instead of crashing in production.

How do I handle API responses with uncertain data shapes?

Use unknown for the response type, then write a type guard to validate the response structure before accessing fields. This pattern safely handles APIs that may return unexpected or missing properties without relying on any.

Can I gradually migrate from any to unknown in existing TypeScript code?

Yes. Replace any declarations with unknown incrementally, adding type guards where needed. This gradual approach maintains type safety improvements without requiring a full codebase rewrite, making migration manageable for large projects.

What kind of type guards work best for validating external data?

Use runtime validators that check property existence, types, and expected values. Type guard functions return a boolean and narrow the type, allowing TypeScript to treat validated data as safely typed after the guard passes.