microsoft / microsoft/TypeScript
Detect dead exports as well as per-module dead code
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Search Terms
dead code
Suggestion
Add a feature to detect dead code not just within modules, but within the build as a whole.
I've looked at https://github.com/Microsoft/TypeScript/issues/16939 which is currently locked so can't comment there. This really does need to be part of the Typescript compiler as no other program is really going to understand all the subtleties; the emitted JS doesn't contain all the information about interfaces and that kind of thing. Furthermore, this is not a file level operation- you're talking about tracking each individual interface/function/etc and not files. With these two factors it's no surprise that there are no current tools that can accomplish this- you would have to reimplement the compiler.
This would probably not be useful for library projects, as by definition things they export are not dead, therefore a separate compiler switch would be useful that application developers can enable.
The exact behaviour should be that exported top-level declarations may be considered "dead" if they are not visibly imported, as well as if they are not used from within the current module. The compiler will error on unused exports. The compiler should detect dynamic imports if the module path is hardcoded- the compiler already supports proper type inference in this case so feels like the compiler can already figure this one out.
This does not have to extend to properties/members of classes or anything like that- that can be a future improvement.
The compiler should ignore exports with a leading underscore, offer a built-in decorator to override a declaration as used, or both to handle any weird cases or entry points.
Use Cases
We already detect dead code across individual files, but not across our whole project. We wish to do so across the entire project. We currently inspect runtime metadata of modules imported/exported as a unit test; unfortunately this is limited because TypeScript will emit modules that don't have any runtime code (e.g. just define an interface) but won't emit references to them. In addition, this approach can only detect whole modules as dead and not parts of them.
Examples
The user would simply turn this on in their tsconfig.json, mark up the entry point for their app, then run the build.
Checklist
My suggestion meets these guidelines:
- [*] This wouldn't be a breaking change in existing TypeScript/JavaScript code- existing users don't have to turn on the flag if they don't want to and it probably shouldn't be part of --strict either.
- [*] This wouldn't change the runtime behavior of existing JavaScript code
- [*] This could be implemented without emitting different JS based on the types of the expressions
- [*] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- [*] This feature would agree with the rest of TypeScript's Design Goals.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No source files or tests are named. Start by reviewing the compiler's existing per-module unused-code diagnostics and how tsconfig.json options are processed, then investigate how project-wide imports, exports, and hardcoded dynamic imports could be tracked. Done would mean a documented compiler option that reports unused exported declarations while respecting application entry points and the proposed exceptions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 18/100