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:
module.exports = {
parser: '@typescript-eslint/parser',
extends: [
'plugin:react/recommended',
'plugin:@typescript-eslint/parser',
],
parserOptions:{
ecmaVersion: 2018,
ecmaFeatures: {
jsx: true,
},
rules: {}
settings: {
react: {
version: 'detect'
}
}
}
}
- Install the required dev dependencies for prettier:
yarn add prettier eslint-config-prettier eslint-plugin-prettier -D
- Create a
.prettierrc.js
file at the root path and setup the Prettier config:
module.exports = {
semi: true,
trailingComma: 'all',
singleQuote: false,
printWidth: 80,
tabWidth: 2,
}
- Update
.eslintrc.js
:
module.exports = {
parser: '@typescript-eslint/parser',
extends: [
'plugin:react/recommended',
'plugin:@typescript-eslint/parser',
'prettier/@typescript-eslint', // add this line
'plugin:prettier/recommended', // add this line
],
parserOptions:{
ecmaVersion: 2018,
ecmaFeatures: {
jsx: true,
},
rules: {}
settings: {
react: {
version: 'detect'
}
}
}
}
- 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
}