TypeScript helps an AI coding agent by turning some hidden assumptions into precise errors before the program runs. The agent can write code, run the checker, read the exact broken contract, and repair a smaller problem.
A mistake the checker catches
Create a new folder, initialize it, and install the tested compiler:
mkdir typescript-agent-feedback
cd typescript-agent-feedback
npm init -y
npm install --save-dev typescript@5.9.3Save this as broken.ts:
function lineTotal(price: number, quantity: number): number {
return price * quantity;
}
console.log(lineTotal("19.90", 2));Run npx tsc --noEmit --strict broken.ts. TypeScript reports that a string cannot be passed where a number is required. That feedback arrives before this path reaches a user.
Now save the complete correction as corrected.ts:
type PricePayload = { price: number; quantity: number };
function isPricePayload(value: unknown): value is PricePayload {
if (typeof value !== "object" || value === null) return false;
const candidate = value as Record<string, unknown>;
return typeof candidate.price === "number" && typeof candidate.quantity === "number";
}
function lineTotal({ price, quantity }: PricePayload): number {
return price * quantity;
}
const externalJson: unknown = JSON.parse('{"price":19.9,"quantity":2}');
if (!isPricePayload(externalJson)) throw new Error("Invalid price payload");
console.log(lineTotal(externalJson).toFixed(2));Run npx tsc --strict --outDir dist corrected.ts && node dist/corrected.js. The expected output is 39.80.
The useful loop for an agent
- Write one bounded change.
- Run
npx tsc --noEmit --strict. - Give the exact error to the agent.
- Repair the contract instead of suppressing the error.
- Run the checker and the relevant test again.
Types do not prove that external JSON is valid. TypeScript erases types when it produces JavaScript, so data from an API, file, form, or model still needs a runtime check. Tests and human review still cover behavior that types cannot prove.
See the idea visually
Watch the original BetterMeir EP007 video.
Tested with TypeScript 5.9.3 and Node.js 26.0.0 on 13 Sep 2026.
