//Install TS and eslint as dev dependencies locally
$ npm install typescript --save-dev
$ npm install express
$ npm install --save-dev eslint @types/express @typescript-eslint/eslint-plugin @typescript-eslint/parser
$ npm install --save-dev ts-node-dev
//Add "tsc": "tsc" to scripts
{
// ..
"scripts": {
"tsc": "tsc",
"dev": "ts-node-dev index.ts",
"lint": "eslint --ext .ts ."
},
// ..
}
//Initialise tsconfig.json settings
npm run tsc -- --init
tsc --init
//Template tsconfig.json file
{
"compilerOptions": {
"target": "ES6",
"outDir": "./build/",
"module": "commonjs",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": false,
"noFallthroughCasesInSwitch": true,
"esModuleInterop": true,
"resolveJsonModule": true
}
}
//Another option for a tsconfig.json file
"compilerOptions": {
"module": "system",
"strictPropertyInitialization": true,
"useUnknownInCatchVariables": true,
"strictFunctionTypes": true,
"allowUnusedLabels": false,
"allowUnreachableCode": false,
"noPropertyAccessFromIndexSignature": true,
"exactOptionalPropertyType": true,
"forceConsistentCasingInFileNames": true,
"target": "es5",
"module": "commonjs",
"rootDir": ".",
"outDir": "../dist/",
"sourceMap": true
}
//.eslintrc.js file
module.export = {
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.eslint.json",
tsconfigRootDir: __dirname,
sourceType: "module",
},
};
//tsconfig.eslint.json
{
"extends": "./tsconfig.json",
"include": [
"src",
"tests",
".eslintrc.js"
]
}
Comments