Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Typescript with Angular CLI and Express/Node in the same project?

I am building a skeleton MEAN project using the Angular CLI. Here is a look at the folder structure and the tsconfig.json file.

enter image description here

Right now, the server code, in the server directory, is written in JavaScript and I would like to be able to write it in TypeScript.

In development, I usually run the following commands:

nodemon server.js
ng build --watch

I am wondering what changes do I need to make in order to be able to write my server code in TypeScript? Do I need to create a separate tsconfig.json file somewhere else to deal with the server code or can instructions for Angular and server code be contained in the same tsconfig.json file? Can I keep using the 2 above commands for my development? Do I need to add a new command? Ideally, I would prefer a solution that doesn't use tools like Gulp but sticks to NPM scripts, if need be.

like image 592
snowfrogdev Avatar asked Oct 18 '22 12:10

snowfrogdev


1 Answers

Here is my solution. I have added a second tsconfig file in the root directory that I named tsconfig-server.json. Here is a look at the content of this file:

enter image description here

What's more, I have changed my NPM start script to run the following:

"start": "ng build -watch | tsc -p tsconfig-server.json -watch | nodemon server.js"
like image 126
snowfrogdev Avatar answered Oct 21 '22 00:10

snowfrogdev