9

I have a quick question on the tsc command being used with the arguments --build --clean, which I understand is used for cleaning /wiping off the .js files generated earlier by the Transpiler (tsc).

What is the speciality or significance of this command? If at all I need to remove all the .js files, I can easily accomplish it through rm -rf *.js or del *.js in the directory.

Can someone educate me on the missing pieces if any?

2 Answers 2

16

The difference is that rm will happily delete any files, even if they weren't generated by transpiling TypeScript.

NOTE!

tsc --build --clean will only delete .js files if there is a corresponding .ts file that it would have been generated from. This means that, if you rename .ts files, delete .ts files, or create new .ts files, and then run tsc --build --clean, old .js files that previously had corresponding .ts files will no longer be deleted.

If you really want to clean up before a build, especially when the set of .ts files has changed, you should instead delete your output folder a different way. For example rm -rf dist/ in a unix shell, or rm -r -fo dist/ in Windows PowerShell. Or, assuming you probably use Node.js if you use TypeScript, use a cross-platform package like rimraf to delete your output folder.

10
  • 1
    @itsraghz tsc knows what .ts files your project has, so it knows what .js files it would create when building. Those are the ones --clean will then delete.
    – ruohola
    Jun 26, 2021 at 17:49
  • 2
    I just made a quick test. Looks like it deletes the .js files matching with the .ts files. I created a dummy.ts and ran tsc to get a dummy.js and then deleted the original dummy.tsc before I ran the tsc --build --clean just to find out the fact that the dummy.js file is left as it is :) so this must be it? Thank you for the good scenario to play around! Are there any other differences - or is that all?
    – itsraghz
    Jun 26, 2021 at 18:04
  • 1
    Yeah, it's completely stateless. If you create dummy.ts and dummy.js, --clean will delete the .js file even if it wasn't actually created by building the TypeScript.
    – ruohola
    Jun 26, 2021 at 19:54
  • 1
    --clean is ofc always preferable since it's platform agnostic (rm won't likely work on Windows), and it cannot accidentally delete some important files.
    – ruohola
    Jun 26, 2021 at 19:55
  • 1
    Perfect. Thank you @ruohola, I agree with your words.
    – itsraghz
    Jul 4, 2021 at 8:15
14

The difference is --clean is useless in general. If you have ever renamed, moved, or deleted a source file, clean will not clean up the transpiled output.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.