Create React project
https://create-react-app.dev/docs/adding-typescript/.
Create a React + TypeScript project using the following command.
|
|
For resource referencing purposes, add the baseUrl
attribute to tsconfig.json
.
Configure ESLint and Prettier
Install ESLint
|
|
ESLint is a code checking tool to check whether your code conforms to a specified specification.
ESLint Initialization.
|
|
Configure on demand, for both React and TypeScript.
Since the ESLint configuration uses some variables and simple judgments to make the configuration easier, I chose the JavaScript configuration. Using the popular Airbnb rules.
|
|
Description of the role of each package.
- eslint-plugin-react: React syntax rules
- @typescript-eslint/parser: TypeScript syntax parser
- @typescript-eslint/eslint-plugin: TypeScript syntax rules
Installing Additional Packages
Further enhancements to Airbnb using TypeScript.
|
|
Automatic deletion of unused imports.
|
|
Install Prettier
|
|
Prettier is a code formatting tool. Used in conjunction with ESLint, it can format code according to the rules set by ESLint.
Install Prettier-related dependencies for ESLint.
|
|
Description of the role of each package.
- eslint-config-prettier: disables ESLint rules that conflict with Prettier.
- eslint-plugin-prettier: Run Prettier as an ESLint rule so that code can be fixed automatically with ESLint’s
--fix
.
Create configuration file .prettierrc
.
Edit .eslintrc.js
to configure ESLint.
Complete ESLint configuration example.
|
|
Add ESLint exception .eslintignore
to exclude files or directories that do not require lint.
Automation
To facilitate batch formatting of code, you can add an automated formatting script to package.json
.
|
|
Then you can use yarn run lint
and yarn run format
to batch check and fix errors.