How to Find Unused Code in Your TypeScript Project Fast
Part of the Dead Code guide. Start with The function passed its test. The application never called it.
An unused export and an unused function are different things. The function might still run inside its own file, in which case removing the export could be enough.
That distinction gets harder when a helper comes through a barrel, a test imports it, another package exposes it, and the report still says unused. I'd rather investigate one finding properly than approve a large deletion because the output looked convincing.
Start with Knip. Get a report, check what the analysis includes, then decide what to remove.
Run Knip before changing the configuration
From the repository root, follow Knip's manual installation instructions:
npm install -D knip typescript @types/node
npx knip
The current Knip v6 requires Node.js 20.19.0 or later. Its docs also list Bun as an option.
knip reports unused files, exported symbols, and dependencies. Start with its defaults, look at the reported paths, check a few findings against actual callers, and only add configuration where the default analysis needs help.
TypeScript can also report unused locals and parameters.
Turn on noUnusedLocals and noUnusedParameters in tsconfig.json to enable those checks.
For a reported export, check whether the declaration is still used within its file, whether another workspace consumes it, whether it belongs to a public package API, and whether the expected consumer exists at all. Those answers lead to different edits. Keep the implementation when only its export is unnecessary.
Read the report at the right workspace boundary
Here's a real report from IsaacScript's public monorepo, recorded in an upstream Knip issue, run with npx knip, using Knip 2.40.1, in November 2023, at revision aa5b9e68ca3ec147d0b87ec83da8bd8a536dcf61.
This is a historical capture, trimmed below. It isn't a fresh scan or a count of the project's current unused code.
Unused dependencies (53)
@algolia/client-search package.json
@arktype/fs package.json
The maintainer said @arktype/fs was used and declared in packages/isaacscript-common-node. Knip treated the root declaration as unused; the package still had a consumer.
Knip's current issue guidance describes the same distinction. Before removing a dependency declaration, check which manifest the report names, which workspace imports the package, whether that workspace declares it, and what the root manifest still needs.
Give monorepo workspaces their own entry points
knip reads workspace definitions from package manifests and pnpm-workspace.yaml.
Its workspace configuration can add workspaces those files don't list. Each workspace needs a package.json.
Start without a config. If you need custom boundaries, this is a knip.json template for a repository with root scripts, an API application, and shared packages:
{
"workspaces": {
".": {
"entry": ["scripts/*.ts"],
"project": ["scripts/**/*.ts"]
},
"apps/api": {
"entry": ["src/main.ts!", "src/workers/*.ts!"],
"project": ["src/**/*.ts!", "!src/test-helpers/**!"]
},
"packages/*": {
"entry": ["src/index.ts!"],
"project": ["src/**/*.ts!", "!src/test-helpers/**!"]
}
}
}
These are example paths. The template assumes an API starting at src/main.ts, independently launched workers under src/workers/, package entry files at src/index.ts, and root maintenance scripts under scripts/. Change them to match how your repository actually starts.
The root workspace is ".". In a workspace project, top-level entry and project options are ignored; put root settings inside that workspace.
Knip's entry-file documentation explains that custom patterns replace the defaults. Plugins can still add entry files, package manifests can identify others, your explicit patterns need to preserve the real starting points, and marking every source file as an entry will hide useful findings.
Check exports that only tests use
knip includes tests in its default analysis. A test import can keep a production export from appearing unused, even when the application never calls it.
Run the production analysis as a second view:
npx knip --production
In the template above, the trailing ! marks production patterns, and !src/test-helpers/**! keeps test helpers out of the production run.
knip's test-runner plugins already leave test files out of production mode.
Its project-files guide advises against negating tests in project. Adapt the helper path to your repository.
Test-related plugin entries are excluded in production mode. Keep both views.
If mocks or other test support still show up as unused files in production mode, add a negated pattern ending in ! for them too.
If a helper appears only in the production report, check whether it's a test utility, an implementation exported solely for testing, a deliberately public API, or a feature that hasn't been connected. Don't delete test support just to clear a report.
Our example of exports with only a test caller examines that last case.
Follow barrels, CommonJS and import side effects
A re-export can be part of a public API
An index.ts barrel adds a re-export between the implementation and its consumer. Follow that chain before changing the implementation.
knip normally protects exports and re-exports in entry files because consumers may live outside the repository.
Read Knip's includeEntryExports reference before treating a package API as unused.
For a self-contained application, the CLI reference documents an opt-in check:
npx knip --include-entry-exports
Use it after reviewing the entry files. In a published library, an export with no in-repository consumer may still be part of the contract your users depend on.
CommonJS needs a closer look
Knip's CommonJS guide documents a named-export tracking gap with this form:
const mod = require("./mod");
mod.fn();
Knip doesn't trace mod.fn() back to the named export, so it can report fn as unused even though the code calls it.
Destructuring the name with const { fn } = require("./mod") gives Knip a reference it can follow.
For computed imports, inspect the runtime loader and configure its real entry files. Read Knip's dynamic-import guidance.
Bundle pruning doesn't authorize source deletion
webpack uses sideEffects metadata to decide which modules it can omit from a bundle. That decision concerns the bundle it produces.
A file can matter for code it runs during import, even when nobody consumes its exports. Check registration modules, CSS imports, polyfills, and any initialization the application relies on before removing the file. Setting "sideEffects": false is a promise about those effects, so it isn't a way to silence an unused-code report.
Put the reviewed check in CI
Once you've reviewed the initial findings, commit the configuration and dependency lockfile. Add this step after checkout and dependency installation in your existing workflow:
- name: Check unused code
run: npx knip
knip exits with code 1 when it reports issues, as its CI guide documents. Get the local run into a state you understand first, preserve intentional exceptions in configuration, review new findings with the diff, and keep the normal build and tests alongside it.
Review the deletion separately.
For each removal, check the actual consumers and exercise the affected behavior before merging.
Use the AI coding review guide for the review beyond this check.
Let your agent investigate a finding
I build Pharaoh. Its get_unused_code tool reports Dead, Possibly unused, and Used only by tests, so an agent can investigate findings with those distinctions visible.
Dead requires every signal to agree. No entry point reaches the function, no other file names it, and no production file imports its file. If an unreached function is named in another file, or its file is imported, it's reported as Possibly unused, with the files that name it.
Pharaoh's map covers the default branch, not other branches or uncommitted edits. Use the local Knip run to check your cleanup changes.
For the agent workflow, read letting your agent check for dead code.
Connect a repository to use it with your agent.
What about ts-prune and tsr?
The tsr repository is archived. Its README says the project has ended.
ts-prune's current README says development has resumed after a hiatus. Older advice describing both projects as archived needs updating.
I'd start this workflow with Knip and one finding. Identify the consumer you expected to exist, trace the imports that remain, check the public API boundary, and make the smallest removal those checks support.