This configuration uses create-react-app and airbnb's style guide as the base style guide.
node 16+
npm 8+
yarn
yarn create react-app [name] --template typescript
Ensure to use newest version of the yarn (at leaset v. 3)
yarn set version stable
Defines what linker should be used for installing Node packages
nodeLinker: node-modules
# yarn
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
# Optional eslint cache
.eslintcache
# Logs
logs
*.log
yarn add --dev prettier
{
"arrowParens": "avoid",
"printWidth": 100,
"semi": false,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5",
"useTabs": false
}
yarn prettier --check ./src
It should print some formatting errors - this is ok. Continue with rest of the setup before fixing them.
yarn add --dev eslint eslint-config-airbnb eslint-config-airbnb-typescript eslint-config-prettier eslint-config-standard-with-typescript eslint-plugin-import eslint-plugin-n eslint-plugin-promise eslint-plugin-react
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"airbnb",
"airbnb-typescript",
"plugin:react/jsx-runtime",
"prettier"
],
"overrides": [
],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module",
"project": "./tsconfig.json"
},
"plugins": [
"react"
],
"rules": {
"@typescript-eslint/semi": ["error", "never"]
}
}
3.3. Disable the default eslint rules specified by "create-react-app" by deleting "eslintConfig" object in package.json.
{
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
}
}
yarn eslint --ext .jsx,.js,.tsx,.ts ./src
It should print some errors like "XXXX problems (XXXX errors, XX warnings)"
yarn add --dev husky lint-staged
4.2. Set up a pre-commit hook using husky & lint-staged to make sure that every commit is formatted.
yarn husky install
yarn husky add .husky/pre-commit "yarn lint-staged"
"scripts": {
...
"prepare": "husky install"
},
{
....
"lint-staged": {
"src/**/*.{js,jsx,ts,tsx,json,css,scss,md}": "prettier --write",
"src/**/*.{js,jsx,ts,tsx}": "eslint --cache --max-warnings=0"
}
}
git add .
git commit -m "initial setup"
It should trigger pre-commit hook and show messages similar like below
✔ Preparing lint-staged...
❯ Running tasks for staged files...
❯ package.json — 1 file
✔ src/**/*.{js,jsx,ts,tsx,json,css,scss,md} — 1 file
❯ src/**/*.{js,jsx,ts,tsx} — 1 file
✖ eslint --cache --max-warnings=0 [FAILED]
↓ Skipped because of errors from tasks. [SKIPPED]
✔ Reverting to original state because of errors...
✔ Cleaning up temporary files...
✖ eslint --cache --max-warnings=0:
...
✖ 6 problems (6 errors, 0 warnings)
✔ Preparing lint-staged...
❯ Running tasks for staged files...
❯ package.json — 1 file
✔ src/**/*.{js,jsx,ts,tsx,json,css,scss,md} — 1 file
❯ src/**/*.{js,jsx,ts,tsx} — 1 file
✖ eslint --cache --max-warnings=0 [FAILED]
↓ Skipped because of errors from tasks. [SKIPPED]
✔ Reverting to original state because of errors...
✔ Cleaning up temporary files...
husky - pre-commit hook exited with code 1 (error)