- Run
sudo npm i -g typescript
to install typescript globally. - Check typescript version by running
tsc --version
to make sure you installed it correctly.
- Create a file called app.ts
touch app.ts
- Write typescript code in it.
- To compile it run
tsc app.ts
. It will generate an actual Javascript file for app.js. - If you want to watch your ts file every time when changes happen, run
tsc app.ts -w
.
- Create a typescript config file by running
tsc --init
. This will generate a tsconfig.json file with commented options. - Make the
"target"
toes2016
- Uncomment
"outDir": "./"
and"rootDir": "./"
. - Add
./dist
to outDir."outDir": "./dist"
: It will add the compiled js files into dist folder. - Add
./src
to rootDir."rootDir": "./src"
: It will specify the root directory of input files. - Make sure to uncomment the
"moduleResolution": "node"
- Now move the
app.ts
file intosrc
folder. - Simple run
tsc
in the project directory to automatically create adist
folder and add the compiled version ofapp.ts
.
- Add package.json file by running
npm init -y
. - Install express:
npm i express
. - Run
npm i -D typescript ts-node nodemon @types/node @types/express
to install typescript, ts-node, nodemon, types definition of node and express as Dev dependencies.
Add Below as Scripts in package.json file
"start":"node dist/app.js",
"dev": "nodemon --exec ts-node src/app.ts",
"build": "tsc -p ."
Run npm run dev
to start development, after you finish your work, you can run npm run build
to compile the files inside the src
folder into javascript files (create a dist folder to store them). In short, the src
folder contains the typescript
files and the dist
folder contains the compiled (into javascript) version of typescript files.