Home / Software Development / TypeScript Refactoring Prompt That Explains Every Change
Software DevelopmentTypeScript 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.
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
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
| Variable | What 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
- Paste one function or small module, not a whole repository.
- Describe the context, especially whether strict mode is on.
- Read the problem list before the code. If the model misunderstood the intent, correct it and rerun.
- Go through the behavior-change list and decide which changes you accept.
- 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
| Situation | What to change |
|---|---|
| Legacy JavaScript | Ask the model to migrate to TypeScript in two passes: types first, refactor second. |
| Performance focus | Add "Identify the time complexity before and after and note any memory trade-offs." |
| Small safe change | Add "Make the smallest possible change; do not rename anything." |
| Code review only | Remove steps 2 and 3 and ask for a prioritized list of issues. |
Common mistakes
- Pasting too much at once. Large inputs lead to skipped parts and silent changes. Work function by function.
- Skipping the behavior list. This list is the reason to use the prompt. If you only read the code, you lose the safety it provides.
- Merging without running tests. The model cannot run your code. Your tests are the real verification.
- Not stating the TypeScript settings. Advice that is right in strict mode may be noise or wrong without it. State your configuration.
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.