| name | dce |
| description | Detect and eliminate dead code in TypeScript projects using ts-remove-unused (tsr). Use when the user wants to find unused exports, unused files, or clean up dead code. |
| user-invocable | true |
Dead Code Elimination
Overview
This document explains how to detect dead code in TypeScript projects.
Tool: ts-remove-unused (tsr)
Installation and Execution
npx -y tsr [options] [...entrypoints]
npx -y @line/ts-remove-unused
Basic Usage
- Check help
npx -y tsr --help
- Check with single entrypoint
npx -y tsr 'src/index\.ts$'
- Check with multiple entrypoints
npx -y tsr 'src/index\.ts$' 'src/cli/cli\.ts$'
- Check including test files
npx -y tsr 'src/index\.ts$' 'src/cli/cli\.ts$' 'test/.*\.ts$' 'src/.*_test\.ts$'
Options
-w, --write: Write changes directly to files
-r, --recursive: Recursively check until project is clean
-p, --project <file>: Path to custom tsconfig.json
--include-d-ts: Include .d.ts files in the check
Real Analysis Example
1. Initial Run
$ npx -y tsr 'src/index\.ts$'
Results:
- 67 unused exports
- 15 unused files
2. Run including CLI
$ npx -y tsr 'src/index\.ts$' 'src/cli/cli\.ts$'
Results:
- Unused files reduced to 14 (excluding those used by CLI)
3. Run including test files
$ npx -y tsr 'src/index\.ts$' 'src/cli/cli\.ts$' 'test/.*\.ts$' 'src/.*_test\.ts$'
Results:
- Unused files reduced to 4 (excluding those used in tests)
Interpreting Analysis Results
Types of Unused Exports
-
Type Definitions (oxc_types.ts)
- Many AST types are exported but unused
- Action: Export only actually used types
-
Internal Utility Functions
- Example:
getNodeLabel, getNodeChildren (apted.ts)
- Action: Remove
export as they are internal implementation
-
Helper Functions
- Example:
collectNodes, findNode (ast_traversal.ts)
- Action: Consider if needed as public API
Types of Unused Files
-
Test-only Files
*_test.ts files
- Action: Include as test entrypoints
-
Duplicate Functionality
- Example:
function_body_comparer.ts (integrated elsewhere)
- Action: Delete
-
Experimental Code
- Example:
ast_traversal_with_context.ts
- Action: Delete or move to
experimental/
Recommended Workflow
- First run analysis only
npx -y tsr 'src/index\.ts$' 'src/cli/cli\.ts$'
- Review results and decide action plan
- Items that can be deleted
- Items to remove export but keep as internal implementation
- Items to keep for future use
- Clean up incrementally
- First delete obviously unnecessary items
- Then remove
export from internal implementations
- Finally organize type definitions
- Automatic fixes (carefully)
git stash
npx -y tsr --write 'src/index\.ts$' 'src/cli/cli\.ts$'
git diff
Notes
- Dynamic imports: tsr uses static analysis and cannot detect dynamic imports
- Type-only exports:
export type is also detected as unused
- Re-exports: Be careful with barrel files (index.ts)
Example in This Project
-
Unused code found in diagnostics
- Unused imports in
semantic_normalizer.ts
extractSemanticPatterns function (commented out for potential future use)
-
Actions taken
- Removed unused imports
- Kept potentially useful code commented out
-
Results
- Cleaner codebase
- Expected reduction in build size