typescript-refactoring-and-simplification-patterns

Refactor existing TypeScript backend code to reduce complexity while preserving external behavior.

1|1|Updated Apr 8, 2026
One-click install
npx skills add https://github.com/GonkaGate/opencode-setup --skill typescript-refactoring-and-simplification-patterns-gonkagate
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: typescript-refactoring-and-simplification-patterns
Source: https://github.com/GonkaGate/opencode-setup/tree/main/.agents/skills/typescript-refactoring-and-simplification-patterns
Command: npx skills add https://github.com/GonkaGate/opencode-setup --skill typescript-refactoring-and-simplification-patterns-gonkagate

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? TypeScript backend codebases accumulate accidental complexity: tangled handlers, boolean flag parameters, scattered as casts, leaky abstractions, and dead code. This Skill guides behavior-preserving refactors that lower local reasoning cost without hiding behavior changes inside cleanup diffs. ## Core Features & Use Cases - Behavior-Preserving Refactoring: Applies moves like Extract Function, Split Phase, Remove Flag Argument, and Remove Dead Code while explicitly naming the preserved contract (outputs, side-effect order, error behavior, async sequencing). - Boundary Normalization: Moves parsing, validation, and narrowing of untrusted inputs (JSON, env vars, query params, driver output) to a single boundary seam so internals work on trusted shapes. - Complexity Diagnosis: Classifies the dominant problem as data-shape complexity, control-flow sprawl, type/helper complexity, abstraction leakage, or dead surface, then picks the smallest reversible move. - Use Case: Given a 200-line Express handler that parses request bodies, branches on a boolean flag, and calls a database twice, the Skill recommends splitting parse/execute/format phases, replacing the flag with explicit functions, and adding characterization tests before any extraction. ## Quick Start Ask the AI to refactor a specific TypeScript file or handler to reduce complexity without changing its external behavior, and have it state what behavior is preserved.

Frequently Asked Questions about typescript-refactoring-and-simplification-patterns

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

FAQPage Schema
How do I refactor TypeScript code without changing behavior?

First name the preserved contract: return values, error shapes, side-effect order, and async sequencing. Then add characterization tests if behavior is uncertain, choose the smallest reversible move such as Split Phase or Remove Flag Argument, and migrate call sites incrementally before deleting old paths.

How to simplify a large TypeScript handler function?

Identify the dominant complexity source first, usually control-flow sprawl or mixed phases. Split the handler into parse, execute, and format phases with an explicit intermediate type, and move validation of untrusted input to a single boundary seam.

When should I use satisfies instead of as in TypeScript?

Use satisfies for config-like tables when you want compatibility checking without losing literal precision. Prefer guards or assertion functions over as when the real fix is boundary normalization, since as silences the compiler without proving runtime safety.

Does enabling strict TypeScript flags count as a refactor?

No. Flipping flags like noUncheckedIndexedAccess or exactOptionalPropertyTypes is a migration or investigation map, not a behavior-preserving refactor. Treat the new errors as a guide for where invariants are fake, then fix call sites in a separate planned change.

When should I not use behavior-preserving refactoring?

Avoid it when the real win requires architecture or ownership-boundary redesign, greenfield type modeling, public API changes, or when preserved behavior cannot be observed through tests, contracts, or call sites and no small proof seam exists.