To ensure that the coding style is consisitent in the project from different members, for React + Typescript projects, it is recommanded set up ESlint and Prettier.
- Install the required dev dependencies:
yarn add eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-react -D
- Create a
.eslintrc.js
file at the root path and setup the ESlint config:
/**
* @description eslint configuration file parameters,
* @implements rulesets { typescript, prettier }
* @constant
*/
const eslintrc = {
env: {
browser: true,
es2021: true,
},
extends: [
"eslint:recommended",
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended",
"eslint-config-prettier",
],
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 12,
sourceType: "module",
},
plugins: ["react", "@typescript-eslint"],
rules: {},
settings: {
react: {
version: "detect",
},
},
};
export default eslintrc;
- Install the required dev dependencies for prettier:
yarn add prettier eslint-config-prettier eslint-plugin-prettier -D
- Create a
.prettierrc
file at the root path and setup the Prettier config:
{
"arrowParens": "avoid",
"semi": true,
"tabWidth": 4,
"trailingComma": "all",
"singleQuote": false
}
- Ensure your team members' VS Code are installed
ESlint
andPrettier
extensions, open the workspace of the project, then pressCtrl + Shift + P
for Windows orCmd + Shift + P
for Mac, then inputOpen Workspace Settings (JSON)
, VS Code would create.vscode/settings.json
, edit the setttings:
{
"editor.formatOnSave": true
}