Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using TypeScript in an existing Visual Studio Web Site project

I've got an existing Visual Studio project that was setup as a "Web Site" project. You can create projects like this by going to File->New Web Site. This is a different project type than a "Web Application Project".

I'd like to mix in some TypeScript on this project. However, I can't figure out how to instruct Visual Studio to "build" my .ts files and generate .js files. I've got the VS2012 TypeScript plugin, and I'm able to a create a TypeScript project, as outlined here. That project works fine, but that's more along the lines of a "Web Application Project". It's a different project type than a "Web Site Project".

Also, when I create .ts file, inside that file the editor gives me TypeScript syntax highlighting and intellisense. But again, I can't figure out how to get it to compile the TypeScript to JavaScript.

A little help?

like image 244
Josh Avatar asked Oct 12 '12 21:10

Josh


People also ask

Can I use Visual Studio for TypeScript?

TypeScript supportBy default, Visual Studio 2022 provides language support for JavaScript and TypeScript files to power IntelliSense without any specific project configuration.


1 Answers

Visual Studio now supports "compile on save" for TypeScript files. I'm using Visual Studio 2013 Update 2, and I can turn this on in a Web Site project by going to Tools -> Options -> Text Editor -> TypeScript -> Project, and checking the box "Automatically compile TypeScript files which are not part of a project." I don't know why it's labeled like this, because it is clearly compiling files which are part of my project...

Many of the other answers to this are not applicable to Web Site Projects, because you can't hook into the build process. With a Web Site Project, there is no csproj or vbproj file, as the build is handled by IIS, not Visual Studio.

If you're using a Web Site Project, and "compile on save" doesn't work for you, there are only a few alternatives:

1) Use the command line TSC compiler to manually compile your code.

2) Create a custom tool for manually compiling your files. You can configure this for use in Visual Studio.

3) Create an "on-demand compilation" .aspx page that will compile the TypeScript and return it as JavaScript.

4) Use the TypeScript Compile JavaScript project to automatically your code in the browser. This gives you the "on-demand compilation" that normally comes with Web Site Projects.

Before "compile on save" was working in VS, I was using the TypeScript Compile. Now I'm happily using "compile on save."

like image 104
Josh Avatar answered Oct 17 '22 23:10

Josh