Create a project
npx create-react-app myapp --template typescript
Add linting to VS Code
- install
ESlint
extension by Microsoft - Make sure the ESLint is configured to check React and Typescript. Open Preferences->Settings, in the
Workspace
tab, queryeslint:probe
, make sure thattypescript
andtypescriptreact
are on the list.
Add code formatting
- Install Prettier:
npm i -D prettier
- Prettier has overlapping style rules with ESLint, so install the following two libraries to allow Prettier to take responsibility for the styling rules from ESLint:
npm i -D eslint-config-prettier eslint-plugin-prettier`
- The ESLint config needs to be updated to allow Prettier to manage the styling rules. Add the Prettier rules to the
eslintConfig
section inpackage.json
as follows:
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest",
"plugin:prettier/recommended"
]
}
- Prettier can be configured in a file called
.prettierrc.json
. Create this file with the following content in the root folder:
{
"printWidth": 100,
"singleQuote": true,
"semi": true,
"tabWidth": 2,
"trailingComma": "all",
"endOfLine": "auto"
}
VS Code integrates with Prettier to automatically format code when source files are saved
- install extension
Prettier - Code formatter
- Open Preferences->Settings, in the
Workspace
tab, queryformat on save
, make sure thatEditor: Format On Save
option is ticked. - One more settings. Tell VS Code the default formatter to use to format code. In the
Workspace
tab, querydefault formatter
, make sureEditor: Default Formatter
is set toPrettier - Code formatter
.
Or replace the step 2 and step 3 with a VS Code settings file .vscode/settings.json
:
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
}
Start the app in development mode
npm start
Produce a production build
npm run build