You'll notice you can find most of them on the typescript website 😛 Then why'd I do this? Well just for fun 🤷♂️ 😁
JavaScript
NodeJS
npm
src
folder contains all the TS
files and build
folder contains the equivalent JS
and declaration(*.d.ts
) files.
Steps used to initialise a basic typescript node project :
npm init -y
npm install typescript nodemon @types/node --save-dev
npx tsc --init --rootDir src --outDir build --lib es6,dom --module commonjs --allowJs true --esModuleInterop --resolveJsonModule --noImplicitAny true
Step 3 is totally your choice which options you choose to enable and what values you prefer.
Option | Description |
---|---|
--rootDir | Folder where we put our Typescript code /src . Only use to control the output directory structure with --outDir . |
--outDir | Typescript to JavaScript compiled code goes here. |
--module | Specify module system for JavaScript files used in project. |
--esModuleInterop | It instructs TypeScript to allow us to use an import like this import myModule from '../myModule' instead of import myModule = require('../myModule') even if your module system defines CommonJS . source |
--lib | If your target is es5 but you will be using es6 or esnext features then you can specify it in this option. source |
--allowJs | If you have JavaScript in your project, should it be compiled with Typescript. |
--resolveJsonModule | Include modules imported with .json extension. |
--noImplicitAny | Raise error on expressions and declarations with an implied any type. |
Sources: Typescript Handbook | Understanding TypeScript Configuration Options
- Create a
nodemon.json
file as following (So that you don't have to compile again and again after making changes,nodemon
takes care of that.) :
{
"watch": ["src"],
"ext": ".ts,.js",
"ignore": [],
"exec": "npx tsc"
}
- Change
package.json
as following :
"scripts": {
"start": "nodemon",
"test": "echo \"Error: no test specified\" && exit 1"
}
npm start
. Your all the.ts
files get compiled to.js
in build folder and then you can run them usingnode
./build/<your_compiled_file.js>