When I add a new TypeScript file to my solution, the system nicely gives me a (prefilled) .ts file with the .js file just below. This configuration makes me think that the conversion from TypeScript to JavaScript is automatic. This assumption is strengthened by the fact that the .ts file has a Build Action 'TypeScriptCompile'. However, regardless of what I do (save a changed ts, compile the project ...), nothing really happens.
I know I can add prebuild steps to the project but I would really like the TypeScriptCompile build action to automatically kick in so that I just need to save the file for the .js file to be regenerated. What do I need to do to make 'TypeScriptCompile' from working as expected/desired ?
I had the same problem when trying to use TypeScript in an MVC4 project, but resolved the problem!
Put the following lines into your project file using your favorite text editor. There is a commented out BeforeBuild
section usually already there at the end of the proj file, which you can replace with these lines. Reload the project and you are good to go. The .js files will now be generated during compilation.
<Target Name="BeforeBuild">
<Exec Command=""$(PROGRAMFILES)\Microsoft SDKs\TypeScript\0.8.0.0\tsc" @(TypeScriptCompile ->'"%(fullpath)"', ' ')" />
</Target>
Have a good look at the exact path though, it may differ (e.g. if you have a different version of the Typescript SDK). Similarly, if tsc.exe
is already in your PATH
environment variable, you can use a version-agnostic command:
<Target Name="BeforeBuild">
<Exec Command=""tsc" @(TypeScriptCompile ->'"%(fullpath)"', ' ')" />
</Target>
Finally, as noted in the comments, you might want to add -c to preserve comments to the command line. This allows tools like cassette & asp.net's resource bundling to retrieve the references and render things in the correct order.
If you still have problems compiling typescript files, you might want to check out an app I just released. You can compile the files with just 1 click of a button.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With