dce
Dead Code Elimination
Overview
This document explains how to detect dead code in TypeScript projects.
Tool: ts-remove-unused (tsr)
Installation and Execution
# Run directly with npx (recommended)
npx -y tsr [options] [...entrypoints]
# Or, run with old package name (deprecated)
npx -y @line/ts-remove-unused # -> warns to use tsr
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
exportas they are internal implementation
- Example:
-
Helper Functions
- Example:
collectNodes,findNode(ast_traversal.ts) - Action: Consider if needed as public API
- Example:
Types of Unused Files
-
Test-only Files
*_test.tsfiles- Action: Include as test entrypoints
-
Duplicate Functionality
- Example:
function_body_comparer.ts(integrated elsewhere) - Action: Delete
- Example:
-
Experimental Code
- Example:
ast_traversal_with_context.ts - Action: Delete or move to
experimental/
- Example:
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
exportfrom internal implementations - Finally organize type definitions
- Automatic fixes (carefully)
# Take backup before running
git stash
npx -y tsr --write 'src/index\.ts$' 'src/cli/cli\.ts$'
git diff # Check changes
Notes
- Dynamic imports: tsr uses static analysis and cannot detect dynamic imports
- Type-only exports:
export typeis 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 extractSemanticPatternsfunction (commented out for potential future use)
- Unused imports in
-
Actions taken
- Removed unused imports
- Kept potentially useful code commented out
-
Results
- Cleaner codebase
- Expected reduction in build size
More from yutakobayashidev/dotnix
markitdown
Convert files (PDF, DOCX, PPTX, XLSX, HTML, images, audio, etc.) to Markdown using Microsoft's markitdown CLI. Use when the user wants to extract text content from documents, convert files for LLM processing, or read non-text file formats.
25gha-lint
Lint and secure GitHub Actions workflows using pinact, actionlint, ghalint, and zizmor. Use when the user adds, updates, or reviews GitHub Actions workflows and wants to check correctness, security, or pin action versions.
1oura-daily-watch
Build and run a daily Oura + Discord behavior monitor. Use when the user wants morning wellness summaries, anomaly alerts, readiness/sleep trend checks, or advice based on Oura Ring data combined with chat activity patterns.
1speakerdeck
Download slide images from a SpeakerDeck presentation. Use when the user provides a SpeakerDeck URL and wants to read, summarize, or convert the slides.
1check-similarity
Detects duplicate TypeScript/JavaScript code using AST comparison for refactoring. Use when the user wants to find similar or duplicated functions, plan refactoring, or clean up redundant code in TS/JS projects.
1social-digest
Fetch today's Discord channel + Mastodon posts via API tokens, summarize to Markdown, and save into an Obsidian vault (Bun script).
1