Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScriptCompile build action doesn't seem to do anything

Tags:

typescript

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 ?

like image 296
Wvdd007 Avatar asked Oct 02 '12 09:10

Wvdd007


2 Answers

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="&quot;$(PROGRAMFILES)\Microsoft SDKs\TypeScript\0.8.0.0\tsc&quot; @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />
</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="&quot;tsc&quot; @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />
</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.

like image 51
Per Brage Avatar answered Nov 18 '22 04:11

Per Brage


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.

like image 1
Arrow Avatar answered Nov 18 '22 03:11

Arrow