<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/fix-ai-code-hallucinations-typescript-compiler-tdd-ucvfxgpnp" -->

---
title: Fix AI Code Hallucinations: TypeScript Compiler + TDD
description: AI coding agents generating TypeScript often produce code that looks plausible but fails to compile because they draw on probabilistic training data rather...
canonical: https://daily.dev/posts/fix-ai-code-hallucinations-typescript-compiler-tdd-ucvfxgpnp
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Fix AI Code Hallucinations: TypeScript Compiler + TDD | daily.dev
og:description: AI coding agents generating TypeScript often produce code that looks plausible but fails to compile because they draw on probabilistic training data rather...
og:url: https://daily.dev/posts/fix-ai-code-hallucinations-typescript-compiler-tdd-ucvfxgpnp
og:image: https://api.daily.dev/og/posts/UCvFXGpNP.png
og:image:alt: Fix AI Code Hallucinations: TypeScript Compiler + TDD
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Fix AI Code Hallucinations: TypeScript Compiler + TDD

**[SitePoint](https://daily.dev/sources/sitepoint)** · 18 min read · 1 upvotes · 0 comments

## Summary

AI coding agents generating TypeScript often produce code that looks plausible but fails to compile because they draw on probabilistic training data rather than the real type system. A proposed harness extracts TypeScript compiler diagnostics via the Compiler API, enriches them with enclosing symbol context using ts-morph, validates the payload with Zod schemas, and merges it with structured Vitest test failures into a compact JSON feedback object fed back into the agent's context window. The approach reduces token usage per error from roughly 110 to 42 tokens, raises first-pass fix rate from 54% to 87%, and cuts average iterations to zero errors from 3.8 to 1.6 in an unblinded 50-error sample across three open-source TypeScript projects. The piece also covers thrashing detection via hashing, CLI flag configuration, integration patterns with Claude Code and Cursor, and limitations such as runtime-only bugs and agent-authored tests that can rubber-stamp hallucinated expectations.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.sitepoint.com/ai-code-hallucinations-typescript-compiler-diagnostics-tdd>

## Questions this post answers

### Why do AI coding agents like Claude, GPT-4, or Copilot generate TypeScript code that doesn't compile?

They draw on probabilistic training data rather than the actual type system, producing fabricated method signatures, phantom generic parameters, invented overloads for standard library functions, and calls to third-party APIs that were never published. Feeding compiler diagnostics and test failures back as structured JSON forces the agent to self-correct against the real type system instead of its memory of it.

_Developers debugging AI-generated TypeScript errors can find related workflow write-ups on daily.dev._

### How much does structuring TypeScript compiler errors as JSON instead of raw tsc output save in tokens?

Structured JSON diagnostic frames use roughly 42 tokens per error compared to about 110 tokens for raw tsc terminal output, measured with tiktoken's cl100k_base encoding. Across a 12-error compilation run, this is roughly a 60% reduction in context window consumption, since raw output includes ANSI codes, repeated file paths, and boilerplate summary lines.

_Teams optimizing AI agent token costs can track techniques like this on daily.dev._

### How do you get structured TypeScript compiler diagnostics without parsing tsc CLI output?

Use the TypeScript Compiler API's typescript.createProgram function to get typed diagnostic objects directly in memory, avoiding terminal string parsing, ANSI stripping, or locale-dependent formatting. Call getSyntacticDiagnostics(), getSemanticDiagnostics(), and optionally getDeclarationDiagnostics() (only when declaration or composite is enabled in tsconfig.json) to collect all diagnostics as structured arrays.

_Developers wiring compiler diagnostics into custom tooling can follow implementation guides on daily.dev._

## Similar posts on daily.dev

- [Testing AI-Generated Code: Developer Guide 2026](https://daily.dev/posts/testing-ai-generated-code-developer-guide-2026-twfgjvgmd) · Alex CloudStar · 0 upvotes · 0 comments
- [Spec-Driven Development: Enforcing Agent Architectural Contracts](https://daily.dev/posts/spec-driven-development-enforcing-agent-architectural-contracts-xvqgn1fyd) · SitePoint · 3 upvotes · 0 comments
- [10 anti-AI slop moves for frontend projects going faster than humans can review—Martian Chronicles, Evil Martians’ team blog](https://daily.dev/posts/10-anti-ai-slop-moves-for-frontend-projects-going-faster-than-humans-can-review-martian-chronicles--70jbo6jvx) · Martian Chronicles · 24 upvotes · 5 comments

---

Tags: [#ai-agents](https://daily.dev/tags/ai-agents), [#typescript](https://daily.dev/tags/typescript), [#tdd](https://daily.dev/tags/tdd), [#vitest](https://daily.dev/tags/vitest)

[View this post on daily.dev](https://daily.dev/posts/fix-ai-code-hallucinations-typescript-compiler-tdd-ucvfxgpnp)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Fix AI Code Hallucinations: TypeScript Compiler + TDD","url":"https://daily.dev/posts/fix-ai-code-hallucinations-typescript-compiler-tdd-ucvfxgpnp","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/fix-ai-code-hallucinations-typescript-compiler-tdd-ucvfxgpnp"},"datePublished":"2026-09-20T15:32:34.074Z","dateModified":"2026-09-20T15:33:06.210Z","description":"AI coding agents generating TypeScript often produce code that looks plausible but fails to compile because they draw on probabilistic training data rather...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/20ecaca31501e46341c0b76c22861e78?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/20ecaca31501e46341c0b76c22861e78?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"SitePoint","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"SitePoint","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/sitepoint","url":"https://daily.dev/sources/sitepoint"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/fix-ai-code-hallucinations-typescript-compiler-tdd-ucvfxgpnp","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"ai-agents,typescript,tdd,vitest","timeRequired":"PT18M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"SitePoint","item":"https://daily.dev/sources/sitepoint"},{"@type":"ListItem","position":3,"name":"Fix AI Code Hallucinations: TypeScript Compiler + TDD"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/fix-ai-code-hallucinations-typescript-compiler-tdd-ucvfxgpnp#faq","mainEntity":[{"@type":"Question","name":"Why do AI coding agents like Claude, GPT-4, or Copilot generate TypeScript code that doesn't compile?","acceptedAnswer":{"@type":"Answer","text":"They draw on probabilistic training data rather than the actual type system, producing fabricated method signatures, phantom generic parameters, invented overloads for standard library functions, and calls to third-party APIs that were never published. Feeding compiler diagnostics and test failures back as structured JSON forces the agent to self-correct against the real type system instead of its memory of it. Developers debugging AI-generated TypeScript errors can find related workflow write-ups on daily.dev."}},{"@type":"Question","name":"How much does structuring TypeScript compiler errors as JSON instead of raw tsc output save in tokens?","acceptedAnswer":{"@type":"Answer","text":"Structured JSON diagnostic frames use roughly 42 tokens per error compared to about 110 tokens for raw tsc terminal output, measured with tiktoken's cl100k_base encoding. Across a 12-error compilation run, this is roughly a 60% reduction in context window consumption, since raw output includes ANSI codes, repeated file paths, and boilerplate summary lines. Teams optimizing AI agent token costs can track techniques like this on daily.dev."}},{"@type":"Question","name":"How do you get structured TypeScript compiler diagnostics without parsing tsc CLI output?","acceptedAnswer":{"@type":"Answer","text":"Use the TypeScript Compiler API's typescript.createProgram function to get typed diagnostic objects directly in memory, avoiding terminal string parsing, ANSI stripping, or locale-dependent formatting. Call getSyntacticDiagnostics(), getSemanticDiagnostics(), and optionally getDeclarationDiagnostics() (only when declaration or composite is enabled in tsconfig.json) to collect all diagnostics as structured arrays. Developers wiring compiler diagnostics into custom tooling can follow implementation guides on daily.dev."}}]}
```

