Home / Software Development / TypeScript Refactoring Prompt That Explains Every Change

Software Development

TypeScript Refactoring Prompt That Explains Every Change

Refactor messy TypeScript for readability and type safety, and get an explicit list of behavior changes and tests to add.

3 min read · Published October 18, 2026 · Last reviewed October 18, 2026

The risky part of an AI-assisted refactor is not the new code, it is the changes you did not notice. This prompt asks the model to review before it rewrites, and to list every place where behavior could differ.

That makes the result reviewable. You read a short report, then the diff, instead of comparing two blocks of code by eye.

The prompt

Copy and paste
Act as a senior TypeScript engineer doing a careful code review and refactor.

Step 1: List the problems in the code below (naming, duplication, weak types, hidden side effects, unclear control flow).
Step 2: Provide the refactored code. Preserve behavior. Do not add dependencies. Do not change exported names or signatures unless you flag it.
Step 3: List every behavior change, however small (for example loose vs strict equality, changed defaults, error handling).
Step 4: Suggest the tests that would catch a regression in the refactored code.

Context: {CONTEXT}

Code:
{CODE}

What to replace

VariableWhat to put there
{CONTEXT}Framework, TypeScript strictness, and any style rules, for example "strict mode, no classes".
{CODE}One function or module at a time gives the best results.

How to use it

  1. Paste one function or small module, not a whole repository.
  2. Describe the context, especially whether strict mode is on.
  3. Read the problem list before the code. If the model misunderstood the intent, correct it and rerun.
  4. Go through the behavior-change list and decide which changes you accept.
  5. Run your existing tests, then add the suggested ones before merging.

Worked example

This example was written for this page to show the format. Results from your own model and your own material will differ, so run the prompt yourself before relying on it.

Input

function getTotals(orders: any) {
  let result: any = {};
  for (let i = 0; i < orders.length; i++) {
    if (orders[i].status == 'paid') {
      if (result[orders[i].customerId]) {
        result[orders[i].customerId] += orders[i].amount;
      } else {
        result[orders[i].customerId] = orders[i].amount;
      }
    }
  }
  return result;
}

Output

Problems: "any" types hide mistakes; repeated array indexing; loose equality; the if/else duplicates the assignment; the name does not say what is totaled.

interface Order {
  customerId: string;
  amount: number;
  status: 'paid' | 'pending' | 'refunded';
}

export function getPaidTotalsByCustomer(
  orders: readonly Order[],
): Record<string, number> {
  const totals: Record<string, number> = {};
  for (const { customerId, amount, status } of orders) {
    if (status !== 'paid') continue;
    totals[customerId] = (totals[customerId] ?? 0) + amount;
  }
  return totals;
}

Behavior changes: (1) the function is renamed, so callers must be updated; (2) "!==" replaces "==", which only matters if status could be a non-string; (3) a customer with a first amount of 0 is now handled correctly.
Tests to add: no orders; only unpaid orders; two paid orders for one customer; a paid order with amount 0.

Variations

SituationWhat to change
Legacy JavaScriptAsk the model to migrate to TypeScript in two passes: types first, refactor second.
Performance focusAdd "Identify the time complexity before and after and note any memory trade-offs."
Small safe changeAdd "Make the smallest possible change; do not rename anything."
Code review onlyRemove steps 2 and 3 and ask for a prioritized list of issues.

Common mistakes

Frequently asked questions

Is it safe to paste proprietary code?

Check your company policy and your AI provider's data terms first. When in doubt, use a minimal reproduction with names changed.

Does this work for other languages?

Yes. Replace "TypeScript" with your language and adjust the examples in the problem list.

Why ask for tests?

A refactor is only safe if something can fail when behavior changes. The test suggestions point you at the riskiest cases.

More prompts