Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mix Node.js and Typescript in the same Visual Studio project?

Visual Studio has a Typescript addon for developing apps with the Typescript language.

And there is also the Node.js Tools for VS, with which one is able to create and debug Node.js applications.

I tried creating a Node.js project, but then I couldn't add Typescript files to it, and if I added them by hand, they did not get compiled to javascript (couldn't set their Build action to TypeScriptCompile.)
And if I create a simple Typescript project, then it does not start my app with Node, because it is basically just an ASP.Net web project.

Is it possible to somehow create a VS project that uses Typescript and Node.js integration at the same time?

like image 564
Mark Vincze Avatar asked Jan 09 '14 16:01

Mark Vincze


People also ask

Can we use js and ts in same project?

You can use thousands of existing JavaScript libraries in your TypeScript project. Type definition files allow you to enjoy the type-checking and autocomplete features in libraries that were written in JavaScript. These files make you more productive in writing code.

Can you combine js and ts?

The TypeScript compiler supports a mix of JavaScript and TypeScript files if we use the compiler option --allowJs : TypeScript files are compiled. JavaScript files are simply copied over to the output directory (after a few simple type checks).

Can I use TypeScript with node js?

TypeScript is well-established in the Node. js world and used by many companies, open-source projects, tools and frameworks. Some of the notable examples of open-source projects using TypeScript are: NestJS - robust and fully-featured framework that makes creating scalable and well-architected systems easy and pleasant.

Can I use TypeScript and JavaScript at the same time?

Syntax. TypeScript doesn't consider any JavaScript code to be an error because of its syntax. This means you can take any working JavaScript code and put it in a TypeScript file without worrying about exactly how it is written.


1 Answers

Yes, it is possible, but in a somewhat awkward way (I hope Node.js Tools will support this better in the future).

You should create Node.js project and add a new file. Choose new text file and enter its name with .ts extension. It will create a typescript file.

At this point you will have a typescript files without compiled javascript.

To fix this go to VS "Tools" menu, open "Options" and go to "Text editor" > "TypeScript" > "Project" and under "Compile on Save" check "Automatically compile TypeScript files which are not part of a project and choose "Use commonjs...".

After that open each .ts file and save it - tsc will compile it to javascript.

Downside of this solution is that you have to remember to save manually each .ts you edit, but I think its not a big deal.

Here's a link to the post that helped me with the same problem: http://blog.ctaggart.com/2013/11/nodejs-tools-with-typescript-console-app.html

Update

If you grab the latest dev build of Node.js Tools you will see that TypeScript support is added. you can create TypeScript project (and this includes debugging .ts files feature)

https://nodejstools.codeplex.com/discussions/531222

like image 128
Slawek Avatar answered Oct 24 '22 16:10

Slawek