How to delete compiled JS files from previous typescript(.ts) files? How to delete compiled JS files from previous typescript(.ts) files? angular angular

How to delete compiled JS files from previous typescript(.ts) files?


I came here seeing the title, and adding gulp into the mix was not a solution. So here is how I solved it.

Prefix your npm build script with rm -rf ./js/ &&

"scripts": {    ...    "build": "rm -rf ./js/ && tsc",    ...},

rm -rf ./js/ forcefully removes recursively all files and dirs below ./js/ doku rm in bash

&& says if successfully do the next command && in bash


with the latest tsc, you should be fine to do the clean with below command

tsc --build --clean

My current tsc version for your reference

$ tsc --versionVersion 3.5.3

Note that --clean flag is part of project-references and works only with it.


This solution is inspired in @Akash Kurian Jose's answer. Since his solution depends on the OS you're using, I'm adding this one which will work on any OS:

Add rimraf as a dev dependency:

"devDependencies": {   "rimraf": "^3.0.1"}

Then add these 3 scripts to your package.json:

"rimraf": "./node_modules/rimraf/bin.js","clean" : "rimraf js/","compile": "npm run clean && tsc -p ./"

When executing npm run compile the js/ folder will be cleaned before compiling.