ts-remove-unused is a command line tool for TypeScript projects that auto-fixes unused `export`s. It removes the export keyword from the declaration or the whole declaration based on its usage in the project.
There are some similar tools but they are focused on "detecting" rather than "removing" so I've built one myself. I wanted a solution that's as minimal as possible; config files to specify the files in your project shouldn't be necessary because that info should be already configured in tsconfig.json. All you need to do is to specify your entrypoint file.
Feedback is much appreciated!
It deleted 100s of files, most of which were Jest test files, and potentially all of which were a mistake. I restored them all with `git restore $(git ls-files -d)`.
I then ran `tsc` on the remaining _modified_ files and `Found 3920 errors in 511 files.`
Obviously at that point I had no choice but to discard all changes and unfortunately I would not recommend this for others to even try.
BTW the VSCode extension which someone else linked to discovered everything perfectly, which is another hint that your tool needs to improve, not your users.
Even if tsconfig.json was very important, and the users are all wrong and you're right, the only thing your readme says is: "The CLI will respect the tsconfig.json for loading source files." which is insufficient.
Most transformations like this are not possible with pure static analysis and require some domain knowledge (or repo-specific knowledge) in order to pull off correctly. This is because some code gets "used" in ways that are not apparent i the code.
Enjoy!
I've never interacted with git programatically so I don't know how messy it would be to implement. But for tools that mostly operate on "whole files" rather than lines in files, I guess it shouldn't be that tricky?
Dead Comment
But other than that it's pretty nice, I might look into the code to see if I can help with that small bug.
[1]: https://github.com/nadeesha/ts-prune
[2]: https://github.com/webpro-nl/knip
OP, can you describe differences from knip?
- In general, I personally do not like the idea of having to add another config file in my repo. I feel it's contradictory that I need to add more to my codebase to clean up the clutter in my codebase. I understand that relying on tsconfig to specify the target files has its pros and cons but I hope that my tool will encourage users to maintain their tsconfig. - Knip has its own ecosystem around removing unused stuff in your codebase (not limited to exports). I personally prefer tools that are more single-purposed and does one thing right so I don't have any ideas for expanding features; It will be focused on auto-fixing unused code caused by unncessary `export`s.
I’ve tried tools like this in the past within projects that have high test coverage but I have never had any luck because of this edge case.
https://www.typescriptlang.org/docs/handbook/project-referen...
If you are providing a library, it's possible you are exporting a function that is meant to be used by downstream code, and that function is isolated from other parts of the code (so never used by other functions but only tests)
If you are writing "product" code, most likely this is just dead code. But there are also edge cases where a function is used as the entry point for other code outside the current repository etc.
Put it this way -- if you are given a codebase you have never seen before, and you see a function only imported by test on the first day. Would you remove it without hesitation? Probably not.
I feel this is likely something that must require human experience to be done "correctly".
Unused imports is perhaps the one area where I would be comfortable removing unused imports. I would never personally allow a third party package into our environment for something like this. I really don’t want to be rude about it, but it’s too “trivial” to rely on an external import for. Not because your code couldn’t be better than ours, it probably will be, but because it’s an unnecessary risk.
For smaller or personal projects I think many people will rely on something like prettier to do the same.
You’re right that this tool may not be useful for some codebases. If your modules are more like “scripts” that include side effects, deleting modules just because it’s not referenced may break things. That should not be the case for react projects that are based on components.
In our development process, we don’t allow the changes made by this tool to be deployed automatically. Instead we make sure the changes are okay by creating a pull request and reviewing. We treat it more like an assistant that does all the cumbersome clean up work in no time.
I only see these potential risks:
1. Using a mix of TS/JS and having some blind spots where we could accidentally delete non-dead code without the compiler noticing.
2. Having and relying on side effects. For example, `export const foobar = thisFnWillDoSthImportant()` and then, yes, removing that would break things.
3. Having separate projects/libs where some consumer might be accessing your exports directly.
Do you see other risks than those?
The risk is adding yet another dependency controlled by a third party that executes on your developer’s local machines and on your CI system.