Is there any way to force Sublime text 3 to compile Typescript code on save? It's a bit annoying switching back and forth between terminal and Sublime. Thanks!
4 Answers
I don't know how to do it on save but you can do it by clicking ctrl+b. In order to make it work do this:
- go to Tools->Build System->New Build System
copy and paste the following:
{ "cmd": ["tsc","$file"], "file_regex": "(.*\\.ts?)\\s\\(([0-9]+)\\,([0-9]+)\\)\\:\\s(...*?)$", "selector": "source.ts", "windows": { "cmd": ["tsc.cmd", "$file"] } }
save it as "myTypeScript.sublime-build"
Tools->Build System and choose the build system that you just created "myTypeScript.sublime-build"
every time you want to compile just click on ctrl+b
You can see seome explanation about it and more here

- 3,118
- 1
- 28
- 46
-
How can we set options for compile? – Gaurav Kumar Singh Sep 29 '17 at 10:26
Open a terminal window to the folder that contains your .TS files. Execute 'tsc -w' and let it monitor changes to any of the .ts files and it will compile them to .js . You can use the latest command line typescript for nodejs (make sure to install nodejs and the typescript for it). Leave the terminal window open until you wanna quit. No need any compile on save feature. That option is rarely honored anyway. And the typescript package within sublime text 3 is quite out of date.

- 61
- 1
- 3
Thanks! This works for single file. But I forgot to mention that I have several scripts in the folder. Fortunately, found another solution on the web. In case anyone else will need it:
- create a new file in the same folder where all scripts are located (/js in my case) and name it tsconfig.json
- Paste this code into the file:
{ "compilerOptions": { "emitDecoratorMetadata": false, "module": "commonjs", "target": "ES5" }, "files":["your_script_0.ts", "your_script_1.ts"], "exclude": [ "node_modules" ] }
in terminal navigate to the source folder (/js in my case) and use the following commands:
- for one-time compilation:
tsc -p .
- for compilation on save:
tsc -w
- for one-time compilation:
Please, correct me if I'm wrong or not quite right about the use of the commands. But at least the solution works for me.

- 77
- 2
- 10
For sublime text users
- install package controller from this site.
- open sublime text and press
ctrl+shift+p
and write install package and press enter. - write
Sublimeonsavebuild
and press enter. - go to
preferences > package settings > Sublimeonsavebuild > setting - user
and paste below code and save that.
{ "filename_filter": "(/|\\\\|^)(?!_)(\\w+)\\.(ts|sass|less|scss)$", "build_on_save": 1 }
- go to
tools > build system > new build system
and paste below code, save that as "Typescript" and select this build.
{ "cmd": ["tsc","$file"], "file_regex": "(.*\\.ts?)\\s\\(([0-9]+)\\,([0-9]+)\\)\\:\\s(...*?)$", "selector": "source.ts", "windows": { "cmd": ["tsc.cmd", "$file"] } }
- Now whenever you save the .ts file its automatically compile to .js file.
- Enjoy!!

- 16,638
- 10
- 62
- 73