Setting Up a Rock-Solid Typescript React Project with ESLint and Prettier
Hey fellow devs! š Letās talk about setting up a React project thatās both fun to work with and maintainable. Iāll share our setup that keeps our codebase clean and consistent.
The Foundation: ESLint + Prettier = ā¤ļø
First things first - weāre using ESLint and Prettier together. Why both? ESLint catches potential bugs and enforces coding standards, while Prettier makes everything look pretty (pun intended). Theyāre like the dynamic duo of code quality!
// .eslintrc.json
{
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"plugins": ["react", "@typescript-eslint", "prettier"],
"rules": {
"prettier/prettier": "error",
"react/react-in-jsx-scope": "off",
"no-unused-vars": "warn",
"@typescript-eslint/explicit-module-boundary-types": "off"
}
}
Letās break down this ESLint config:
extends: Weāre building on top of recommended configs for ESLint, React, and TypeScript. Theprettierconfig at the end ensures ESLint plays nice with Prettier.plugins: These add extra rules and functionality - weāve got plugins for React, TypeScript, and Prettier integration.
Now with the rules:
prettier/prettier: "error": Forces Prettierās formatting as ESLint rulesreact/react-in-jsx-scope: "off": Not needed in modern React with automatic JSX runtimeno-unused-vars: "warn": Yellow squiggles instead of red for unused variables@typescript-eslint/explicit-module-boundary-types: "off": Lets us skip return type annotations when TypeScript can infer them
Time to look at the .prettierrc file:
// .prettierrc
{
"semi": false,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5"
}
And hereās what our Prettier config does:
semi: false: No semicolons! JavaScript has ASI (Automatic Semicolon Insertion)singleQuote: true: Use āsingle quotesā instead of ādouble quotesā for stringstabWidth: 2: Two spaces for indentation (fight me 4-spacers! š)trailingComma: "es5": Add trailing commas where valid in ES5 (cleaner git diffs)
Editor Setup: Making Life Easier
The real magic happens in your editor. Weāre using Cursor (VSCode base) with these essential extensions:
- Prettier ESLint
- Error Lens (because who doesnāt want instant feedback?)
Hereās a crucial part of our settings.json:
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
}
Git Hooks: Your Code Quality Bouncer š«
We use Husky to run checks before commits. Itās like having a bouncer that makes sure no messy code gets into your repo.
npx husky add .husky/pre-commit "npm run lint && npm run format"
And in your package.json:
{
"scripts": {
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
"format": "prettier --write .",
"prepare": "husky install"
}
}
CI Pipeline: The Final Guardian
Our CI pipeline (using GitHub Actions) runs these checks on every PR:
name: Code Quality
on: [pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install dependencies
run: npm ci
- name: Run linting
run: npm run lint
- name: Check formatting
run: npm run format:check
Why This Matters
- Consistency: No more debates about tabs vs spaces or semicolons
- Productivity: Auto-formatting on save = less time fixing formatting
- Code Quality: Catch bugs before they make it to production
- Team Happiness: New team members can jump right in without learning custom style guides
Pro Tips š
- Run
npx eslint --initwhen starting a new project to get a good base config - Use
.eslintignoreand.prettierignorefor files you donāt want to check - Consider using TypeScript - it plays really well with this setup
- Keep your dependencies updated (but test before updating in production!)
Common Gotchas to Watch Out For
- ESLint and Prettier fighting each other (solution: use
eslint-config-prettier) - Git hooks not running (check your Husky installation)
- Different formatters in different editors (standardize on Prettier!)
Wrapping Up
This setup might seem like overkill at first, but trust me - itās worth it. Your future self (and your team) will thank you for setting up these guardrails early.
Remember: Good code isnāt just about working features - itās about maintainability and consistency. These tools help you achieve both without thinking about it.
Happy coding! š
P.S. Feel free to steal this setup for your projects. Thatās what weāre here for!