ESLint & Prettier for React & TypeScript

2022. 11. 23.·🎨 프론트엔드 공부/기타

내가 쓰려고 만든 ESLint & Prettier 사용법 (for React & TypeScript)

 

ESLint

1. package.json 확인

- eslint 관련 패키지가 설치되어 있는지 확인

- CRA의 경우 eslint config도 내장되어 있음

- Vite의 경우 내장 X (230824 현재는 내장되어 있음)

 

2. (없는 경우)패키지 설치 및 CLI 설정 실행

npm i -D eslint
npm init @eslint/config
// 또는
yarn create @eslint/config

 

CLI 방식으로 설정하는 이유

- React나 TS를 사용할 때 필요한 플러그인도 함께 다 설치해줌

 

3. 순서대로 아래처럼 입력

사용할 프레임워크 선택
TS 안쓰면 No
JS로 해도 됨
설치되는 플러그인(React, TS)
npm을 선택하는 경우

 

4. rule 추가 (널리 쓰이는 rule)

 

"@typescript-eslint/explicit-function-return-type": "off" 를 통해 함수형 컴포넌트의 리턴값 타입 지정을 막을 수 있음

React.FC 사용하지 않는게 좋기 때문

 

    "rules": {
        "react/react-in-jsx-scope": ["off"],
        "react/jsx-uses-react": ["off"],
        "react/jsx-props-no-spreading": ["warn"],
        "react/no-unescaped-entities": ["off"],
        "@typescript-eslint/explicit-function-return-type": "off"
    }

 

Prettier

1. 패키지 설치

- ESLint와 충돌을 방지하는 보조 패키지들도 설치

 

npm i -D prettier eslint-config-prettier eslint-plugin-prettier

 

2. 설정 파일 생성

- root 디렉토리에 .prettierrc 파일 생성 후 아래 rules 입력

 

{
    //  prettierrc의 전체 스키마를 보여줌
    "$schema": "http://json.schemastore.org/prettierrc",
    "tabWidth": 2,
    "semi": true,
    "singleQuote": false,
    "trailingComma": "all",
    "printWidth": 80,
    "useTabs": false,
    "endOfLine":"auto"
}

 

3. eslint config 변경

- Prettier를 사용하기 때문에 ESLint에 반영해주어야 함

 

env에 추가
"jest": true

extends에 추가
"react-app",
"react-app/jest",
"plugin:prettier/recommended"

parserOptions에 추가
"project": "./tsconfig.json"

plugins에 추가
"@typescript-eslint",
"prettier"

 

⭐4. 최종 .eslintrc.json

 

{
    "env": {
        "browser": true,
        "es2021": true
    },
    "extends": [
        "plugin:react/recommended",
        "standard-with-typescript",
        "plugin:prettier/recommended"
    ],
    "overrides": [
    ],
    "parserOptions": {
        "ecmaVersion": "latest",
        "sourceType": "module",
        "project": "./tsconfig.json"
    },
    "plugins": [
        "react",
        "@typescript-eslint",
        "prettier"
    ],
    "rules": {
        "react/react-in-jsx-scope": ["off"],
        "react/jsx-uses-react": ["off"],
        "react/jsx-props-no-spreading": ["warn"],
        "react/no-unescaped-entities": ["off"],
        "@typescript-eslint/explicit-function-return-type": "off"
    }
}

 

.js 버전 완성본

module.exports = {
  env: {
    browser: true,
    es2021: true,
  },
  extends: [
    "plugin:react/recommended",
    "standard-with-typescript",
    "plugin:prettier/recommended",
  ],
  overrides: [],
  parserOptions: {
    ecmaVersion: "latest",
    sourceType: "module",
    project: "./tsconfig.json",
  },
  plugins: ["react", "@typescript-eslint", "prettier"],
  rules: {
    "react/react-in-jsx-scope": ["off"],
    "react/jsx-uses-react": ["off"],
    "react/jsx-props-no-spreading": ["warn"],
    "react/no-unescaped-entities": ["off"],
    "@typescript-eslint/explicit-function-return-type": "off",
  },
};
'🎨 프론트엔드 공부/기타' 카테고리의 다른 글
  • [Gatsby & Contentful] sourcing rich text from Contenful
  • Gatsby Image components - StaticImage vs GatsbyImage
  • Vite - 매우 빠른 React 개발 환경
  • [Web] jwt 작동 원리
지식물원
지식물원
지식이 자라는 식물원!
  • 지식물원
    지식물원
    지식물원
  • 전체
    오늘
    어제
    • 분류 전체보기 (510)
      • 🎨 프론트엔드 공부 (247)
        • JS & TS (86)
        • HTML & CSS (22)
        • React & Next (49)
        • Vue & Nuxt (22)
        • 기타 (68)
      • 🤓 기술 학습 & 공부 기록 (116)
        • Node.js (0)
        • Python (37)
        • 백엔드 (0)
        • 딥러닝 (1)
        • 컴퓨터 일반 (72)
        • 개발 인프라 (6)
      • 👨‍💻 프로젝트 경험 (6)
        • Work (0)
        • Toy (6)
      • ⚙️ 개발 팁 & 노하우 (21)
        • 프론트엔드 (6)
        • 기타 (15)
      • ☕️ 커리어 & 인터뷰 준비 (88)
        • 코딩 테스트 (88)
      • 📰 기술 트렌드 & 생각 정리 (4)
      • 📚 기타 (25)
        • 마케팅 (15)
        • 비개발서적 (10)
  • 블로그 메뉴

    • 태그
  • 링크

  • 공지사항

    • 모바일 접속 시 코드 하이라이팅 깨질 때
  • 인기 글

  • hELLO· Designed By정상우.v4.10.3
지식물원
ESLint & Prettier for React & TypeScript
상단으로

티스토리툴바