Create a project
npx create-react-app myapp --template typescript
Add linting to VS Code
- install
ESlintextension by Microsoft - Make sure the ESLint is configured to check React and Typescript. Open Preferences->Settings, in the
Workspacetab, queryeslint:probe, make sure thattypescriptandtypescriptreactare 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
eslintConfigsection inpackage.jsonas 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
Workspacetab, queryformat on save, make sure thatEditor: Format On Saveoption is ticked. - One more settings. Tell VS Code the default formatter to use to format code. In the
Workspacetab, querydefault formatter, make sureEditor: Default Formatteris 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