typescript-refactoring

Use this skill when improving existing TypeScript or JavaScript code without intentionally changing product behavior. Reach for it whenever the user says a file or module is too big, messy, brittle, hard to test, full of duplication, deeply nested, over-coupled, confusingly named, legacy, or in need of cleanup, restructuring, simplification, or technical-debt reduction. Also use it when the user asks how to safely split a large component, untangle a service, reduce complexity, isolate side effects, simplify types, or plan a refactor, even if they never say the word "refactor."

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "typescript-refactoring" with this command: npx skills add iamkhan21/skills/iamkhan21-skills-typescript-refactoring

TypeScript Refactoring

What This Skill Does

Use this skill to make code easier to understand, test, and change while preserving behavior unless the user explicitly asks for behavior changes.

This skill is especially helpful when the user says things like:

  • "clean this up"
  • "this component is doing too much"
  • "this file is too big"
  • "please untangle this"
  • "reduce complexity without changing behavior"
  • "make this easier to test"
  • "how should I restructure this?"

Quick Start

  1. Identify the dominant smell before changing structure.
  2. Define scope, boundaries, and non-goals.
  3. Choose one refactoring strategy that fits the smell.
  4. State the safety net: tests, characterization tests, or explicit verification steps.
  5. Make one conceptual transformation at a time.
  6. Verify after each meaningful step.

Response Shape

When responding to the user, prefer this structure:

  1. Name the main pain point or smell.
  2. State what is in scope and what is not.
  3. Explain the first refactoring move and why it is the safest useful step.
  4. Explain how behavior will be preserved.
  5. Apply or describe incremental changes.
  6. Call out risks, tradeoffs, and logical next steps.

Core Workflow

1. Diagnose Before You Rearrange

Do not start with generic cleanup. First decide what kind of problem this is.

  • Long function or deep nesting
  • Duplication
  • Mixed responsibilities
  • Side effects tangled with business logic
  • Poor naming masking unclear responsibilities
  • Complex or misleading types
  • Module-boundary or architecture pressure
  • Legacy code with weak tests or unclear behavior

If the request is vague, translate it into a concrete target smell before making changes.

2. Define Boundaries

Identify the junction points between the code being changed and the rest of the system.

  • Preserve public entry points by default unless the user wants API changes.
  • Name what must stay behaviorally stable.
  • State what is out of scope.
  • If architecture might change, start with the smallest seam that buys safety.

See references/01-preparation.md and references/02-planning-workflow.md.

3. Choose A Proportional Safety Net

Use the best available safety net instead of assuming ideal test coverage.

  • If good tests exist, run and extend them around the touched behavior.
  • If tests are weak, add characterization tests around boundaries and risky cases.
  • If tests are missing, slow down and use narrow changes plus explicit manual verification checkpoints.
  • Treat compiler errors, type errors, and lint failures as guardrails, not annoyances.

Do not use missing tests as permission for a rewrite.

4. Refactor In Small, Reversible Steps

Prefer a sequence of understandable moves over a clever rewrite.

  • One conceptual transformation at a time
  • Keep behavior stable
  • Re-run relevant checks after meaningful changes
  • Keep test refactors separate from production refactors when practical
  • Stop when the original pain point is materially reduced

See references/03-process-heuristics.md.

High-Value First Moves

SymptomFirst moveThen read
Long functions, deep nestingExtract a pure helper or add guard clauses to flatten control flow07-conditions, 06-duplication
Hard to testIsolate side effects from decision logic and identify the seam that blocks testing10-side-effects, 09-abstraction
Copy-paste codeClassify duplication as identical, similar, or conceptual before extracting anything06-duplication
Unclear namesRename to reveal intent and check whether bad names hide mixed responsibilities05-names
Complex types / genericsSimplify contracts first; reduce any, assertions, or overly clever conditional types12-generics, 16-static-typing
Tangled modulesIdentify dependency direction and extract a stable seam or contract13-module-integration, 14-architecture
Imperative loops everywhereUse a clearer data flow only if it improves readability, not because pipelines look nicer08-functional-pipeline, 15-declarative-style
Messy error handlingSeparate expected failures from exceptional ones and clarify handling boundaries11-error-handling
Outdated formatting / lint issuesTake easy compiler, formatter, and lint wins before deeper refactors04-low-hanging-fruit
Large-scale migration pressureStart with a façade, seam, or strangler-style boundary, not a big-bang rewrite14-architecture

Real-World Cases

Low-Test Or Legacy Code

When behavior is unclear, caution is part of the refactor.

  • Add characterization tests for the behavior that already exists.
  • Name the risky behavior surface before changing structure: inputs, outputs, side effects, ordering, fallbacks, errors.
  • Preserve suspicious legacy behavior unless the user explicitly wants a bug fix.
  • Document assumptions and unknowns instead of bluffing certainty.
  • Prefer narrow seams and reversible steps.

When describing the safety net, name a few concrete checks rather than saying "add tests" in the abstract.

Vague Cleanup Requests

If the user says "make it cleaner" or "tidy this up":

  • identify the likely dominant smell
  • state the non-goals
  • choose the smallest change that meaningfully reduces pain

Do not turn a vague request into a broad redesign.

Refactor + Feature Requests

If the user asks for both refactoring and new behavior:

  • acknowledge both goals
  • separate structural cleanup from feature work
  • prefer refactor-first then feature, or at least clearly distinct phases
  • make the packaging explicit: separate commits, review sections, or staged phases
  • keep the first phase small and local

Do not present combined feature work as a pure refactor.

TypeScript-Specific Guidance

  • Use type errors as feedback about contracts and design pressure.
  • Prefer narrowing, helper types, and clearer domain types over as any or broad assertions.
  • Preserve useful caller-facing type information during incremental changes.
  • Avoid introducing generic abstractions until simpler concrete types stop being enough.

See references/16-static-typing.md and references/12-generics.md.

Do Not Do This

  • Do not mix bug fixes or feature work into a refactor unless the user asked for it.
  • Do not rename, redesign, and repartition code all at once unless a smaller sequence is clearly impossible.
  • Do not introduce new layers, interfaces, or patterns before you can explain the coupling or duplication they remove.
  • Do not widen types just to make the code compile.
  • Do not force declarative or architectural patterns when a simpler local change solves the problem.
  • Do not keep refactoring after the original pain point is clearly addressed.

Techniques Index

LevelFileTechniques
Setup04-low-hanging-fruit.mdFormatting, linting, language/API features
Code05-names.mdNaming problems and solutions
Code06-duplication.mdDRY, extraction techniques
Code07-conditions.mdGuard clauses, complexity reduction
Code08-functional-pipeline.mdComposition, data flow
Design09-abstraction.mdAbstraction levels, leak prevention
Design10-side-effects.mdIsolation, command-query separation
Design11-error-handling.mdError types, strategies
Design12-generics.mdGenerics, composition
Architecture13-module-integration.mdCoupling, cohesion
Architecture14-architecture.mdLayers, dependencies, strangler fig
Architecture15-declarative-style.mdDeclarative patterns
Architecture16-static-typing.mdType design
Support17-test-code.mdRefactoring tests
Support18-comments-docs.mdDocumentation

Principles

  • Diagnose first: pick the smell before picking the move.
  • Small steps: each step should be understandable and reversible.
  • Behavior preservation: refactoring changes structure, not product intent.
  • Scope discipline: keep in-scope and out-of-scope explicit.
  • Practicality over purity: prefer the simplest change that relieves the real pain.
  • Know when to stop: stop when the code is materially easier to change and further cleanup has poor payoff.

Templates And Quick Reference

Language Note

Examples use TypeScript. The principles apply broadly, but code patterns and tooling references here are TypeScript and JavaScript specific.

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

General

frontend-init

No summary provided by upstream source.

Repository SourceNeeds Review
General

react-testing

No summary provided by upstream source.

Repository SourceNeeds Review
General

refactoring

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

frontend-design

Create distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, artifacts, posters, or applications (examples include websites, landing pages, dashboards, React components, HTML/CSS layouts, or when styling/beautifying any web UI). Generates creative, polished code and UI design that avoids generic AI aesthetics.

Repository SourceNeeds Review
161.3K94.2Kanthropics