Knip TypeScript 17 Aug 2026

Knip: Stop Agents Writing Code You Don't Need

Agents love writing code that nothing calls yet. Knip finds it, and if you wire it into your verification step, the agent cleans up after itself.


Give an agent a task and it will often build more than the task needs. A helper for the case you haven't hit yet. An export nothing imports. A dependency added for an approach it abandoned halfway through. None of it breaks anything, which is exactly why it survives.

Knip is a TypeScript tool that finds unused files, exports, types and dependencies. That maps almost perfectly onto the kind of premature code agents produce, so it's a cheap way to keep speculative work out of the codebase.

Where it pays off most

New projects. When there's barely any code yet, an agent will happily scaffold a structure it thinks you'll want, and it's hard to spot by eye because everything looks plausible. Knip cuts through that: if nothing reaches it, it doesn't need to exist yet.

It's just as useful after a refactor. Move logic around and the old path often gets left behind, still exported, still compiling, quietly dead. This is where knip differs from a linter: instead of checking one file at a time, it builds a module graph from your entry points and follows the imports. Anything the graph never reaches gets reported, which is exactly the shape of an export nothing calls any more.

Set up the scripts

Install it, then put it behind npm scripts in package.json. Everything else points at these, so there's one place to change how it runs.

npm install -D knip
{
  "scripts": {
    "knip": "knip",
    "knip:fix": "knip --fix"
  }
}

knip reports. knip --fix removes what it can automatically. Keep them separate so you get to read the report before anything is deleted.

Point the agent at it

Add a line to your CLAUDE.md telling the agent to run it as part of verification, alongside your typecheck and tests.

## Verification

Run `npm run knip` before finishing a task. Remove anything it reports that your change introduced. Don't leave exports, files or dependencies behind that nothing uses.

This is the bit that changes the agent's behaviour. It stops being your job to notice the extra code, because the agent gets told about it while it still has the context to deal with it.

Then make it not optional

A CLAUDE.md instruction is a request, not a guarantee. Run the same script in a pre-push hook so nothing leaves your machine without passing, and again in CI so it's enforced no matter who or what wrote the code.

#!/bin/sh
# .husky/pre-push
npm run knip
Note

Because all three layers call the same npm script, the agent, the hook and CI can never disagree about what counts as unused.

Worth pairing with worktrees if you're running several agents at once. Each one verifies its own work in isolation, so knip's report is about that agent's changes and nothing else.