Based on the book by Basarat.
npm install -D typescript @types/node nodemon ts-node tsconfig-paths
npx tsc --init --rootDir ./ --outDir dist --esModuleInterop --resolveJsonModule --lib es6,dom --module commonjs
Source: [https://aka.ms/tsconfig]
Set path alias in tsconfig.json:
{
"rootDir": "./",
"baseUrl": "./src",
"paths": {
"@/*": [ "./*" ]
}
}
In the package.json, set nodemon parameters to fix path alias at compile time:
{
"scripts": {
"build:live": "nodemon -r tsconfig-paths/register --watch 'src/**/*.ts' --exec \"ts-node\" src/index.ts",
}
}
npm i -D jest @types/jest ts-jest
Create a jest.config.js file:
module.exports = {
"roots": [
"tests",
],
"testMatch": [
"**/__tests__/**/*.+(ts|tsx|js)",
"**/?(*.)+(spec|test).+(ts|tsx|js)",
],
"transform": {
"^.+\\.(ts|tsx)$": "ts-jest",
},
}
To run tests:
npm run test
# or
npm run test:watch
Finally, add the new scripts in the package.json file:
{
"scripts": {
"test": "jest",
"test:watch": "jest --watchAll"
}
}
Source: [https://basarat.gitbook.io/typescript/intro-1/jest]
npm install -D prettier
Then, add the scripts in the package.json file:
{
"scripts": {
"prettier:base": "prettier --parser typescript --single-quote",
"prettier:check": "npm run prettier:base -- --list-different \"src/**/*.{ts,tsx}\"",
"prettier:write": "npm run prettier:base -- --write \"src/**/*.{ts,tsx}\""
}
}
To run:
npm run prettier:check
npm run prettier:write
Source: [https://basarat.gitbook.io/typescript/intro-2/prettier]
npm install -D husky
Add the new precommit script in the package.json file:
{
"scripts": {
"precommit": "npm run prettier:write"
}
}
Source: [https://basarat.gitbook.io/typescript/intro-2/husky]
npm install -D eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-jest
Create a .eslintrc.js file:
module.exports = {
parser: "@typescript-eslint/parser",
parserOptions: {
project: "./tsconfig.json",
},
plugins: ["@typescript-eslint"],
extends: [
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended",
],
rules: {
// Overwrite rules specified from the extended configs e.g.
// "@typescript-eslint/explicit-function-return-type": "off",
}
}
Next, add the new lint script in the package.json file:
{
"scripts": {
"lint": "eslint \"src/**\""
}
}
And finally, create a vscode settings.json file:
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"eslint.validate": [
"javascript",
"typescript"
]
}
To run:
npm run lint
Source: [https://basarat.gitbook.io/typescript/intro-2/eslint]
npm install -D standard-version
Add the new release script in the package.json file:
{
"scripts": {
"release": "standard-version",
"postrelease": "git push --follow-tags origin master && npm publish"
}
}
To run:
npm run release
# or
npm run release -- --release-as minor
Source: