unused-code
A function, class, type, interface, constant or enum of the program that nothing used refers to: an export no file of the program imports and that the program doesn't offer outside, or a declaration only such code uses. Idiomatic (on with "idiomatic": true), a warning. Reported by safe-ts check only, which sees the whole program; the editor and the lint plugin check one file and its imports, and can't know who imports a file.
Why: an agent that writes a new function rarely deletes the old one. The old one keeps compiling, keeps its tests, and is read by the next agent as a second way to do the same thing, so the two drift. TypeScript's noUnusedLocals says nothing about exports, and oxlint's no-unused-vars looks at one file: an exported function nobody imports, and the helpers only it calls, pass both. On an adopter's monorepo, 58 such declarations, every one dead on reading: copies of functions a shared package had taken over (the app's copy still exported, the package's used), constants and helpers nothing calls any more, and the chains of helpers only those used.
✅ Accepted: what the entry offers, and what that uses
ts
// main.ts (an entry: `safe-ts check src/main.ts`)
import { total } from "./lib";
export function report(prices: readonly number[]): string {
return `${total(prices)}`;
}
// lib.ts
export function total(prices: readonly number[]): number {
return prices.reduce((sum, p) => sum + round(p), 0);
}
function round(n: number): number {
return Math.round(n);
}❌ Rejected: an export nobody imports, and a helper only it calls
ts
// lib.ts
export function discount(n: number): number { // reported: nobody imports it
return roundDown(n * 0.9);
}
function roundDown(n: number): number { // reported: used only by `discount`
return Math.floor(n);
}text
The function `discount` is exported, but no file of the program imports it, and nothing offers it
outside the program
The function `roundDown` is used only by `discount`, which nothing in the program reachesThe quick fix deletes a declaration nothing refers to at all (with the comment above it). safe-ts fix unused-code src applies it to every such site in one go (--write to write the files): deleting one can leave what only it used unused, which the next run reports and deletes. A declaration used by other dead code, imported somewhere and never used there, or re-exported by a barrel, gets no fix: deleting it alone would break the code that names it.
What counts as used
Everything the module code of a file refers to (what runs when it loads), and everything the outside can use, followed through imports, export { x } from, export *, namespace imports (ns.member; a namespace passed on whole uses every export) and type references. The outside is, by what a file is:
| A file that is | Its exports are used, because |
|---|---|
an entry named on the command line (safe-ts check src/main.ts) | it is what runs, or what is published |
a file nothing in the program imports (every file is an entry in safe-ts check src) | a script, a test, a route a framework finds by its path, a worker |
loaded by import(), new Worker(new URL(...)), import.meta.glob(...) | its exports are read by name at run time |
named by its package.json's main, module, types, typings, exports or bin (or built from it: dist/x.js from src/x.ts) | it is the package's API |
a tool's config file (vite.config.ts, *.config.*) | the tool reads it |
a Vue component, a script without import / export, a declaration file | the template, or every file, reads what it declares |
Not reported either: a class with a decorator (or a decorated member: a framework finds it), a declaration nothing refers to at all and that isn't exported (oxlint's no-unused-vars, in its default correctness category, reports it), and what a file of the same package that the program doesn't reach imports: a test or a script checked separately still uses it. A constant whose initializer runs code (export const store = createStore()) is reported without a fix: its call may be needed.
What it costs
The index is computed, and the files around the program are scanned, only in a run where the rule is on for some file; otherwise the pass does neither. The files outside the program are only parsed, in parallel, and their imports are kept by content: a second check in one process (the language server, a watcher) parses only the files that changed. On an adopter's largest entry: about 30 ms with the rule on (175 files around the program parsed), none of it with the rule off.
Day one
The rule is a warning, and the baseline records the warnings of each rule: write it once (--write-baseline) and the count can only fall. Then safe-ts fix unused-code <dirs> deletes what nothing refers to, a diff to review, and a second run deletes what that left behind.
What it can't see
- Code outside the program and outside its packages. Another repository that imports a published module, a test in a directory with its own package.json that the check doesn't name. Name that code as an entry, or check the directories.
- A name looked up at run time:
module[name]on an object holding the functions (reached through the object: fine) is followed;window[name],eval, a string handed to a framework that looks the export up by name (a route table built from strings) are not. - Templates other than Vue's (Svelte, Angular, an HTML file's inline handlers): a component of theirs that only a template uses looks unused. A Vue component's own declarations are always used.
- Frameworks' conventions other than the ones above: a file a framework loads by its path and that the program also imports (a Nuxt page imported by a test) has its exports judged as a module's. Name it as an entry.
- Class members other than private ones (
unused-private), the members of anEnumthat nothing constructs or matches, object literal members, and the parameters a caller never passes.