The opposite day after I was organising TypeScript with a Node mission, I believed that I might write a easy information that would assist you to doing this in a hopefully easy method.
To start out do the next:
First just remember to have TypeScript put in by working or putting in:
tsc -- model
npm set up -g typescript
Now we have to add a bundle.json file:
npm init -y
So as to add TypeScript run the next command:
yarn add -D typescript
Subsequent, we have to initialize our mission with TypeScript. The command beneath will generate a file referred to as tsconfig.json
. Make certain to learn and perceive this file because it incorporates helpful info and is nicely documented.
npx tsc -- init
Now create a brand new file in your mission and guarantee that the file extension is .ts
. I’ll create a file referred to as app.ts
inside a src
folder. Contained in the file create a console.log
.
src/app.ts
console.log('Hiya World');
Now we have to configure in order that the TypeScript information is compiled into JavaScript information. Go to the file bundle.json
and change the content material within the scripts
block with the next which can execute our TypeScript information and construct them into JavaScript information.
"scripts": {
"construct": "tsc"
}
Now we will construct our mission and after we do that, we are going to get a .js
file which we will use to run our software.
yarn construct // to construct
node app.js // to run
However that is type of annoying to do it this manner after we can run our TypeScript information instantly by including a library who handles this. Run the next command:
yarn add -D ts-node
Now go into your bundle.json
file and the the next line in scripts
:
"scripts": {
"begin": "ts-node src/app.ts",
"construct": "tsc"
}
You possibly can delete the beforehand generated .js
file and now instantly working your TypeScript information:
yarn begin
In case you’re growing and need the modifications to be up to date every time you save, then you’ll be able to add the next library which can restart the server every time you make a change:
yarn add -D ts-node-dev
Now head into the bundle.json
file and replace the scripts.begin
:
"scripts": {
"begin": "ts-node-dev --respawn src/app.ts",
"construct": "tsc"
}
That is it, you might have now arrange TypeScript with Node.
Any feedback, questions or discussions are all the time welcome!