Skip to content

Instantly share code, notes, and snippets.

@sar
Forked from carsenchan/setup.md
Last active February 26, 2021 19:54
Show Gist options
  • Save sar/3d50d4175c7a00a30e3c7d653fa7121b to your computer and use it in GitHub Desktop.
Save sar/3d50d4175c7a00a30e3c7d653fa7121b to your computer and use it in GitHub Desktop.
Eslint + Prettier for React Typescript

Project setup

Purpose

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.

  1. Install the required dev dependencies:
yarn add eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-react -D
  1. Create a .eslintrc.js file at the root path and setup the ESlint config:
/**
 * @description eslint configuration file parameters,
 * @implements rulesets { typescript, prettier }
 * @constant 
 */
const eslintrc = {
    env: {
        browser: true,
        es2021: true,
    },
    extends: [
        "eslint:recommended",
        "plugin:react/recommended",
        "plugin:@typescript-eslint/recommended",
        "plugin:prettier/recommended",
        "eslint-config-prettier",
    ],
    parser: "@typescript-eslint/parser",
    parserOptions: {
        ecmaFeatures: {
            jsx: true,
        },
        ecmaVersion: 12,
        sourceType: "module",
    },
    plugins: ["react", "@typescript-eslint"],
    rules: {},
    settings: {
        react: {
            version: "detect",
        },
    },
};

export default eslintrc;
  1. Install the required dev dependencies for prettier:
yarn add prettier eslint-config-prettier eslint-plugin-prettier -D
  1. Create a .prettierrc file at the root path and setup the Prettier config:
{
    "arrowParens": "avoid",
    "semi": true,
    "tabWidth": 4,
    "trailingComma": "all",
    "singleQuote": false
}
  1. Ensure your team members' VS Code are installed ESlint and Prettier extensions, open the workspace of the project, then press Ctrl + Shift + P for Windows or Cmd + Shift + P for Mac, then input Open Workspace Settings (JSON), VS Code would create .vscode/settings.json, edit the setttings:
{
  "editor.formatOnSave": true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment