14 TypeScript Error Diagnosis Prompts for Cryptic TS Errors

14 copy-paste prompts to diagnose TypeScript errors fast: generics, conditional types, narrowing, module resolution, and .d.ts files. Updated for TS 6.0/7.0, June 2026.

A TypeScript error rarely points at the line that is actually broken. The compiler reports the spot where assignability finally fails, but the root cause is usually one missing property or one bad inference three files upstream. The seven-line Type A is not assignable to type B wall is real, and it scares people into as any, which just relocates the bug to runtime.

The prompts below force the model to walk back from the reported line to the true cause, separate “fix the code” from “fix the types,” and propose the minimum diff instead of a rewrite. Paste in the full error chain plus the surrounding code, not a screenshot or a one-line summary. The second and third lines of a TS error often name the real culprit, so include them.

TL;DR

  • Paste the full error (all lines, with filenames) plus 20-30 lines of code context. The line TS reports is usually downstream of the real mismatch.
  • Use prompt 1 (walk-back) as your default. Reach for the specialized prompts (generics, narrowing, module resolution) when you already know the category.
  • Models that handle these well as of June 2026: Claude Opus 4.7 and Sonnet 4.6 (1M-token context, top SWE-bench Verified at 87.6%), GPT-5.5, and Gemini 3.1 Pro. Big context matters here because TS errors span multiple files.
  • Never let the model “fix” with any or as. Ask explicitly: “fix the underlying shape, not the symptom.”
  • These prompts are version-agnostic, but flag your TS version. Behavior differs across TS 5.9, the 6.0 transition release, and the Go-based 7.0 (beta since April 21, 2026; stable expected around July 2026).

Best for

  • Daily TS development and PR review
  • Debugging library and @types mismatches
  • Migrating JavaScript to TypeScript
  • Onboarding to an unfamiliar TS codebase
  • Surviving a strict flag flip that produces hundreds of new errors

Which model to paste into

Model (June 2026)ContextWhy it fits TS errors
Claude Opus 4.71M tokensBest at multi-file reasoning; SWE-bench Verified 87.6%. Use for gnarly conditional-type and inference chains.
Claude Sonnet 4.61M tokensFaster and cheaper ($3/$15 per 1M vs $5/$25 for Opus); fine for everyday not assignable errors.
GPT-5.5~320 pages in-app (Plus)Strong on terminal/CLI workflows; pick the Thinking mode in the model picker for type puzzles.
Gemini 3.1 Pro1M tokensLimited Pro access on the free Gemini app (daily caps); full 1M context fits a whole tsconfig plus several files on paid Google AI Pro ($19.99/mo).

Context window is the deciding factor: TS errors reference types defined across many files, so a 1M-token window lets you paste the error, the offending file, and the relevant declarations in one shot. In Claude Code or Cursor, the agent reads the files itself, so you can skip the manual paste and just give it the error text.

1. Walk-back diagnosis

Below is a TypeScript error. Walk back from the reported line to find the real cause.
Output exactly: (a) what TS thinks is wrong, (b) what is actually wrong, (c) the minimum
diff to fix it, (d) whether to fix the code or the types. Do not use `any` or `as`.

Error:
[paste the full error, all lines, including filenames]

Code context:
[paste 20-30 lines around the reported line]

2. “Type X is not assignable to Y” diagnosis

TS error: "Type [X] is not assignable to type [Y]". Diagnose: (a) what TS expected,
(b) what it got, (c) the structural diff between X and Y property by property,
(d) is this a real bug or an excess-property / narrowing artifact.

Code:
[paste]

3. Generic-inference failure

TS failed to infer a generic at the call site below. Output: (a) which type parameters
TS tried to infer, (b) which argument made inference fall back to its constraint or
`unknown`, (c) the explicit type argument that fixes it, (d) whether the function
signature itself should change. Note: TS 5.9+ enables --strictInference under --strict,
so flag any inference that newly fails under strict mode.

[paste call site + function signature]

4. Conditional-type debugging

A conditional type is resolving to the wrong branch. Below is the type definition and a
sample input. Walk through each conditional step (`extends` checks, distributivity over
unions, `infer` bindings) and show what TS computes at each step.

[paste type definition + the input type]

5. Module-resolution debug

TS error: "Cannot find module [X]" or "Module [X] has no exported member [Y]". Diagnose
in order: (a) tsconfig `module` + `moduleResolution` (note: `bundler` allows extensionless
imports and reads package.json "exports"; `nodenext` requires extensions and is stricter),
(b) the package's package.json "exports" field and whether the subpath is mapped,
(c) shipped types vs an @types package vs a missing .d.ts, (d) the exact fix.

[paste error + tsconfig compilerOptions + the relevant package.json snippet]

6. Excess-property check confusion

Why does TS allow my extra property when I assign through a variable but reject it when I
pass the object literal inline? Explain TS's excess-property rule (it only fires on fresh
object literals, not on already-typed values), and show the correct fix for my case.

[paste both the passing and failing variants]

7. Narrowing-not-narrowing diagnosis

I have a type guard but TS still complains about the narrowed type below. Diagnose why
narrowing is lost: (a) the guard is not a user-defined type predicate (`x is Foo`),
(b) the value is read through a property access TS treats as mutable, (c) narrowing is
discarded across an `await`, a closure, or a callback, (d) the fix that preserves the
narrow.

[paste]

8. .d.ts / ambient-declaration audit

My project imports [library] but its types are wrong or missing. Walk through: (a) does
the package ship its own types (check package.json "types"/"exports"), (b) is there a
@types/[library] package, (c) do I need a custom .d.ts, (d) give me the skeleton of the
.d.ts to add, with module augmentation if I only need to patch one export.

[paste the imports + the type error]

9. tsconfig “strict” troubleshooting

I turned on [strict | noImplicitAny | strictNullChecks | exactOptionalPropertyTypes |
strictInference] and got [N] new errors. Below are 5 representative ones. For each,
classify it as real bug / type-only / needs refactor, and propose the fix pattern.
Group identical patterns so I can fix them in bulk.

[paste 5 errors + their code]

10. React component prop type fix

My React component has a TS error on its props. Below is the component plus a usage site.
Diagnose: (a) the prop type vs usage diff, (b) a generic-component inference issue,
(c) type loss through an HOC or `forwardRef`, (d) the minimum fix. Prefer typing props
with an explicit interface over inline object types.

[paste component + usage]

11. zod / valibot / runtime-schema to TS type mismatch

My runtime schema ([zod / valibot / yup]) and my TS type are out of sync. Below is the
schema, its inferred type (`z.infer<typeof schema>`), and the hand-written type. Diagnose
the drift and propose how to make one the single source of truth so they can't drift again.

[paste schema + inferred type + manual type]

12. “any” hunt

Below is a file with implicit and explicit `any`s. List each one, explain why TS could not
infer (or why someone added it), and propose the explicit type. Order by risk: the `any`s
that touch external input or function boundaries first.

[paste]

13. Migration: JS to TS, one file at a time

I am migrating [file.js] to TypeScript. Output a step-by-step plan: (a) the types to
introduce first, (b) the union/intersection structure that maps to the runtime shape,
(c) which functions can stay loosely typed for now and a TODO marker for them,
(d) whether to start in `allowJs` mode or convert outright.

[paste the file]

14. TS-error triage by impact

Below are the errors from `tsc --noEmit` (or `tsgo --noEmit` if on TS 7). Triage into:
(a) real bugs (fix code), (b) type-only (fix types), (c) needs refactor, (d) suppress with
`@ts-expect-error` + a reason comment. Order by blast radius: errors that block the most
downstream code first.

[paste]

Common mistakes

  • Reaching for any. It silences the compiler and ships the bug to runtime, where it costs far more to find.
  • Trusting the reported line. It is almost always downstream of the real mismatch; ask the model to walk back.
  • Casting with as blind. A cast that hides a structural mismatch is a silent landmine. Understand the diff first.
  • Disabling strict to make the build green. That throws away the entire reason to use TypeScript. Fix or suppress per-line instead.
  • Pasting one line of the error. TS errors are chains; the second and third lines usually name the actual culprit.
  • Fighting the type system instead of the shape. If the type is hard to express, the runtime shape is often the real problem.

FAQ

Which model is best for diagnosing TypeScript errors in June 2026? For hard multi-file cases, Claude Opus 4.7 leads on SWE-bench Verified (87.6%) and has a 1M-token context that fits the error plus several files. For everyday not assignable errors, Sonnet 4.6 is cheaper and fast enough. GPT-5.5 (Thinking mode) and Gemini 3.1 Pro are strong alternatives; the free Gemini app gives limited Gemini 3.1 Pro access, while its full 1M context comes with paid Google AI Pro ($19.99/mo).

Should I paste the whole file or just the error? Paste the full error (every line, with filenames) plus 20-30 lines of context. If the error references types defined elsewhere, include those declarations too. In an agent like Claude Code or Cursor, skip the paste and let the agent read the files; just give it the error text and the file path.

Does TypeScript 7.0 change how these errors read? The error messages are essentially the same. TS 7.0 (beta since April 21, 2026, stable expected around July 2026) is the Go-based native compiler shipped as @typescript/native-preview with the tsgo binary; it is roughly 10x faster but produces the same diagnostics. The bigger behavior shifts come from TS 5.9’s --strictInference and TS 6.0’s changed defaults, so always tell the model your version.

Why does TS allow extra properties sometimes but not others? Excess-property checking only fires on fresh object literals assigned or passed directly. Once a value is stored in a typed variable, TS treats it as that type and stops checking for extras. Prompt 6 explains the rule and the fix.

How do I stop the model from suggesting as any? Add “do not use any or as; fix the underlying shape” to the prompt, as the templates above do. If a cast is genuinely unavoidable (third-party types you cannot change), ask for as with a comment explaining why it is safe, plus a runtime assertion at the boundary.

Tags: #Prompt #AI coding #AI coding