Show HN: ts-remove-unused – Remove unused code from your TypeScript project
141 points| kazushisan | 1 year ago |github.com
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!
rarkins|1 year ago
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.
kazushisan|1 year ago
_jayhack_|1 year ago
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!
fkyoureadthedoc|1 year ago
zlies|1 year ago
jamil7|1 year ago
filleokus|1 year ago
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?
icholy|1 year ago
[deleted]
worx|1 year ago
But other than that it's pretty nice, I might look into the code to see if I can help with that small bug.
_fat_santa|1 year ago
[1]: https://github.com/nadeesha/ts-prune
[2]: https://github.com/webpro-nl/knip
bikitan|1 year ago
ditegashi|1 year ago
joseferben|1 year ago
Cannabat|1 year ago
OP, can you describe differences from knip?
rickcarlino|1 year ago
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.
kazushisan|1 year ago
https://www.typescriptlang.org/docs/handbook/project-referen...
rty32|1 year ago
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".
devjab|1 year ago
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.
kazushisan|1 year ago
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.
worx|1 year ago
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?
thestephen|1 year ago
One interesting observation: when using it with our Next.js project, it flags all page TypeScript files as unused. This inadvertently highlights a potential drawback of file-system based routing - it can lead to less explicit code relationships.
cdaringe|1 year ago
kazushisan|1 year ago
I've added a more detailed explanation to README so I hope it will change your mind if you've previously had a negative impression :)
https://github.com/line/ts-remove-unused#readme
bhouston|1 year ago
BTW a complimentary tool I've used in the past is depcheck, it is an npm package that removes unused dependencies from your npm package.json file. Smaller package.json files means faster "npm install" and also smaller docker files.
https://www.npmjs.com/package/depcheck
conmalkovic|1 year ago
https://marketplace.visualstudio.com/items?itemName=iulian-r...
danfritz|1 year ago
Has more features (like excluding enums) and works very well in large code bases.
guzik|1 year ago
thiscatis|1 year ago
jjice|1 year ago
istvanmeszaros|1 year ago
For python there is a lib for this, but it is a bit crappy.
theo-steiner|1 year ago
dml2135|1 year ago
That’s a genuine question — I’m only passingly familiar with tree-shaking so I may have a misunderstanding of what it does.
cal85|1 year ago
NathanaelRea|1 year ago
kazushisan|1 year ago
k__|1 year ago
Could this also work with .svelte files, which are essentially html-like files with <script lang="ts">?
kazushisan|1 year ago
aiibe|1 year ago
kazushisan|1 year ago
_aqua|1 year ago
kazushisan|1 year ago
bschmidt1|1 year ago
This looks great, particularly the `skip` and `mode` options (which I'm guessing several commenters here missed).
I suppose it should work just as well with monorepos or any other directory hierarchy right? It only "knows" files are unused because they're never referenced in any code within the defined `projectRoot, and it only knows exports are unused whenever they're never imported?
Cool project, will definitely try it soon.
bowsamic|1 year ago