How Do Types Make Claude's Code More Accurate?
Agree the types and function signatures first, then have Claude implement them. That turns the type checker into a pass or fail check Claude can run itself. In Claude Code, a language server plugin goes further and reports type errors after every edit.
Read [path to existing types or module]. Propose the types and function signatures for [feature]: inputs, outputs, error cases and any new domain types. Where two states can't coexist, use a discriminated union, not optional fields. Don't implement anything yet. After I approve them, implement each function to match those signatures exactly. Don't use `any`, type casts or `@ts-ignore`, or your language's equivalents. If you need a type we didn't agree on, stop and propose it. Finish by running [type-check command] and [test command], and show me the output.
- 1Agree the types
- 2Claude implements
- 3Language server reports errors
- 4Tests check behavior
Try it now
- 1Install your language's server binary. For TypeScript, the docs give
npm install -g typescript-language-server typescript. - 2In Claude Code, run
/plugin install typescript-lsp@claude-plugins-official. The docs list others, includingpyright-lsp,gopls-lspandrust-analyzer-lsp. - 3Ask Claude to introduce a type error in a file and then fix it. You should see
Found N new diagnostic issues in M files (ctrl+o to expand)under the edit, and Ctrl+O shows the details. - 4Send the prompt for a real feature. It worked if the implementation changes no signature you approved, and the output shows the checks passing.
Use it when
Typed code where the data shape matters: transformations, parsers, API boundaries and state machines. In the Claude app, paste your types above the request. Anthropic's prompting docs say long inputs belong at the top and the question at the end, which can improve response quality by up to 30 percent in its tests, especially with complex, multidocument inputs.
Skip it when
Skip the approval step for a first sketch whose shape you're still discovering. The docs suggest /effort low there, since you're steering every step anyway. It also adds little in untyped code with no type checker to run, because nothing would enforce the types you agreed.
Why it works
Anthropic's docs say that when Claude has nothing to run, it can only judge by how finished the work looks, which leaves you catching every mistake. A type checker's exit code fixes that. With a code intelligence plugin installed, Claude also gets the language server's errors and warnings each time it edits a file, so it sees a type error or missing import it introduced without running a compiler. It can also jump to definitions and find references by symbol instead of searching text. Agreeing the shape first also means a wrong mental model shows up in a few lines of types, before any logic depends on it.
Example
For an availability calendar, you agree on type Slot = { start: string; end: string; booked: boolean } and mergeAvailability(a: Slot[], b: Slot[]): Slot[] before any logic exists. If Claude returns the wrong shape, the compiler says so. But it can't tell you whether two returned slots overlap, because overlapping slots are still valid Slot[]. That rule needs a test, so name it in the prompt: no two returned slots may overlap.
Going further
A line in CLAUDE.md saying to always typecheck works most of the time, not every time, as Anthropic's docs put it. To enforce it, add a Stop hook to .claude/settings.json whose command is pnpm tsc --noEmit 1>&2 || exit 2. Exit code 2 sends Claude back to work with the compiler errors, which is why they're redirected to stderr. There's a cap: after eight blocked stops in a row with no tool call between them, Claude Code overrides the next block and ends the turn. CLAUDE_CODE_STOP_HOOK_BLOCK_CAP raises it.
Common mistakes
- Installing the plugin without the binary. The plugin doesn't include the language server, so run
/pluginand look on the Errors tab forExecutable not found in $PATH. - Expecting diagnostics in a cloud session. The docs say Claude Code doesn't start plugin language servers there, so Claude gets no diagnostics or code navigation.
- Trusting diagnostics blindly in a monorepo. The docs list false positives there, where imports are reported as unresolved when they aren't.
- Approving signatures you haven't read. Once you approve them, a wrong shape becomes the target Claude works toward.
Sources
- Code intelligence plugins, Claude Code Docs
- Best practices for Claude Code
- Prompting best practices, Claude Platform Docs
Checked 6 November 2026. Products change often, so confirm details with the vendor.