typescript-refactor

Refactor TypeScript code to reduce unsafe casts and modernize type architecture.

Updated Mar 11, 2026
One-click install
npx skills add https://github.com/selfagency/agentsy --skill typescript-refactor-selfagency
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: typescript-refactor
Source: https://github.com/selfagency/agentsy/tree/main/.agents/skills/typescript-refactor
Command: npx skills add https://github.com/selfagency/agentsy --skill typescript-refactor-selfagency

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) and assets (resource) components.

What problem does it solve?

TypeScript refactors often introduce unsafe type casts, leaky abstractions, and compiler slowdowns, especially when codebases grow and generics and unions become complex.

Core Features & Use Cases

  • Type architecture and modeling: Discriminated unions, branded types, and readonly-by-default patterns to prevent invalid states and domain mix-ups.
  • Safer type narrowing: Custom type guards, assertion functions, and exhaustive switch techniques that eliminate most as casts.
  • Modern TypeScript and performance: satisfies, using-based cleanup, and compiler-friendly rules like avoiding deep recursion and minimizing large union comparisons.
  • Typical use case: When modernizing a backend service, use the rules to redesign error and event types as discriminated unions, enforce exhaustive handling, and reduce type-checking overhead across module boundaries.

Quick Start

Apply the typescript-refactor skill to refactor the TypeScript module by replacing unsafe casts with narrowing, redesigning key domain models using discriminated unions and branded types, and updating the code to modern TypeScript idioms while checking compiler-performance hotspots.

Frequently Asked Questions about typescript-refactor

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

FAQPage Schema
How do I refactor TypeScript code to eliminate unsafe type casts?

To refactor TypeScript code and eliminate unsafe casts, apply custom type guards, assertion functions, and exhaustive switch techniques. These narrowing patterns systematically replace `as` casts by validating types at runtime and enforcing compile-time checks.

What is the best way to prevent invalid states when designing TypeScript domain models?

Designing TypeScript domain models with discriminated unions, branded types, and readonly-by-default patterns prevents invalid states. This architecture encapsulates valid data variations and eliminates domain mix-ups at the compiler level.

Why does my TypeScript compiler slow down with complex generics and large unions?

TypeScript compiler slowdowns often stem from deep recursion and large union comparisons in complex generics. Refactoring involves applying compiler-friendly rules and minimizing structural comparisons across module boundaries to reduce type-checking overhead.

Can I use the `satisfies` operator to modernize TypeScript type checking?

You can modernize TypeScript type checking using the `satisfies` operator to validate expressions against a type without widening it. This ensures type safety while preserving narrow literal types for subsequent property access.

How do I handle resource cleanup using modern TypeScript features?

Handle resource cleanup in modern TypeScript using the `using` keyword for scope-based disposal. This mechanism automatically executes cleanup logic when the execution scope ends, preventing resource leaks without manual try-finally blocks.