- Add typescript to your project as dev dependecy, even if it's installed globally.
npm install typescript -D
- Add this npm script to your package.json for running tsc (Typescript's Native Compiler, it helps us initialize our project by generating our tsconfig.json file)
{
// ..
"scripts": {
"tsc": "tsc"
},
// ..
}
- Initialize tsconfig.json settings by running:
npm run tsc -- --init
You will see a lengthy list of every configuration available. However, most of them are commented out, let at least the following to be active:
{
"compilerOptions": {
"target": "ES6",
"outDir": "./build/",
"module": "commonjs",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"esModuleInterop": true
}
}
- Install express and required eslint packages, also express types:
npm install express
npm install --save-dev eslint @types/express @typescript-eslint/eslint-plugin @typescript-eslint/parser
- Create an .eslintrc file with the following content:
{
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking"
],
"plugins": ["@typescript-eslint"],
"env": {
"browser": true,
"es6": true,
"node": true
},
"rules": {
"@typescript-eslint/semi": ["error"],
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/restrict-template-expressions": "off",
"@typescript-eslint/restrict-plus-operands": "off",
"@typescript-eslint/no-unsafe-member-access": "off",
"@typescript-eslint/no-unused-vars": [
"error",
{ "argsIgnorePattern": "^_" }
],
"no-case-declarations": "off"
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.json"
}
}
- Set up the development environment by installing ts-node-dev :
npm install --save-dev ts-node-dev
Let's add a few more npm scripts to our package.json and we're all set.
{
// ...
"scripts": {
"tsc": "tsc",
"dev": "ts-node-dev index.ts",
"lint": "eslint --ext .ts ."
},
// ...
}
- Run the command below to create a production build:
npm run tsc
- add an npm script to run the project in production mode:
{
// ...
"scripts": {
"tsc": "tsc",
"dev": "ts-node-dev index.ts",
"lint": "eslint --ext .ts .",
"start": "node build/index.js"
},
// ...
}