$ npx create-react-app <프로젝트명> --template typescript
$ npm i --save react react-dom typescript
$ npm i --save-dev @types/react @types/react-dom @types/node
{
"compilerOptions": {
"target": "es6",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"baseUrl": "./src", // 파일을 절대경로로 import 하기 위한 옵션.
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": [
"src"
],
}
$ npm install -D eslint
$ npx eslint --init
How would you like to use ESLint?
What type of modules does your project use?
Which framework does your project use?
Does your project use TypeScript? (y/N)
Where does your code run? (Press space to select, a to toggle all, i to > invert selection)
What format do you want your config file to be in? (Use arrow keys)
The config that you've selected requires the following dependencies:
@typescript-eslint/eslint-plugin@latest @typescript-eslint/parser@latest
Would you like to install them now with npm? (Y/n)
Does your project use TypeScript?(y/N), Would you like to install them now with npm?(Y/n)
의 두 질문에는 Y를 입력하여 관련 플러그인 등을 설치한다.// airbnb의 리액트 관련 규칙 설정 플러그인
$ npm i -D eslint-config-airbnb # 리액트 관련 규칙 O
또는
$ npm i -D eslint-config-airbnb-base # 리액트 관련 규칙 X
// 이외의 플러그인들
$ npm i -D eslint-plugin-react eslint-plugin-react-hooks
$ npm i -D eslint-plugin-jsx-a11y eslint-plugin-import eslint-plugin-prettier eslint-config-prettier
eslint-plugin-import
: ES6의 import, export 구문을 지원하기 위한 플러그인eslint-plugin-react
: React 관련 규칙이 들어있는 플러그인eslint-plugin-react-hooks
: React Hooks 관련 규칙이 들어있는 플러그인eslint-plugin-jsx-a11y
: JSX요소의 접근성 규칙에 대한 정적 검사 플러그인
module.exports = {
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'prettier'],
extends: [
'airbnb', // airbnb-base를 설치한 경우 airbnb-base
'plugin:react/recommended',
'plugin:jsx-a11y/recommended',
'plugin:import/errors',
'plugin:import/warnings',
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
rules: {
'prettier/prettier': ['error', { singleQuote: true }],
'comma-dangle': ['error', 'always-multiline'],
'linebreak-style': 0,
'import/prefer-default-export': 0,
'import/extensions': 0,
'no-use-before-define': 0,
'import/no-unresolved': 0,
'react/react-in-jsx-scope': 0,
'import/no-extraneous-dependencies': 0, // 테스트 또는 개발환경을 구성하는 파일에서는 devDependency 사용을 허용
'no-shadow': 0,
'react/prop-types': 0,
'react/jsx-filename-extension': [
2,
{ extensions: ['.js', '.jsx', '.ts', '.tsx'] },
],
'jsx-a11y/no-noninteractive-element-interactions': 0,
'@typescript-eslint/explicit-module-boundary-types': 0,
},
settings: {
'import/resolver': {
node: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
},
},
},
};
$ npm install -D prettier
{
"singleQuote": true,
"semi": true,
"useTabs": false,
"tabWidth": 2,
"trailingComma": "all",
"printWidth": 80,
"arrowParens": "always",
"orderedImports": true,
"bracketSpacing": true,
"jsxBracketSameLine": false
}
$ npm i styled-components
$ npm i @types/styled-components
$ npm i styled-reset
// globalStyle.tsx
import { createGlobalStyle } from 'styled-components';
import reset from 'styled-reset';
const GlobalStyle = createGlobalStyle`
// Reset CSS
${reset}
* {
box-sizing: border-box;
}
a {
color: inherit;
text-decoration: none;
}
`;
export default GlobalStyle;
// index.tsx
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import GlobalStyle from 'theme/globalStyle';
import App from './App';
ReactDOM.render(
<React.StrictMode>
<GlobalStyle />
<App />
</React.StrictMode>,
document.getElementById('root'),
);
[React] create-react-app & Typescript 초기 세팅 완벽 정리
eslint.org
prettier.io
TSConfig Reference