Dhananjay Patel Logo
  1. Home
  2. / Blog
  3. / Typescript Basic
  4. / Lessons
  5. / 2

Step1 - Init ts.config file

Terminal window
npm install -g typescript #required only first time
tsc --init

Step2 - Update ts.config file

"rootDir": "./src", /* Specify the root folder within your source files. */
"outDir": "./dist", /* Specify an output folder for all emitted files. */

Step3 - Write TS code

Create src/index.ts

index.ts
const myName:string = "Dhananjay"
console.log(myName)

Step4 - Execute

Terminal window
tsc -b #to build the code
node ./dist/index.js #to run the executed file

Every time when index.ts file is modified, step4 is needed to exec the file

If you want to rebuild the code every time you save the code

Solution

1. Init package and install tsc-watch

Terminal window
npm init -y
npm i tsc-watch -D #installing as dev dependency

2. Add dev script

package.json
"scripts": {
"dev": "tsc-watch --onSuccess \"node ./dist/index.js\""
}

3. run npm run dev