yarn add typescript @types/node @types/react @types/react-dom @types/react-router-dom @types/jest
yarn add -D eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser
@types/*** 로 별개로 설치한 라이브러리에 대한 타입 모음을 내려받을 수 있다.
typescript
TS를 JS로 컴파일(트랜스 파일)할 수 있는 컴파일러
@types/node
Node에서 typescript를 사용할 때 모든 유형 정의
@types/react
React에서 typescript를 사용할 때 모든 유형 정의
@types/react-dom
react-dom을 사용할 때의 모든 유형 정의
@types/react-router-dom
react-router-dom을 사용할 때의 모든 유형 정의
@types/jest
jest를 사용할 때의 모든 유형 정의
해당 프로젝트를 컴파일하기 위해 필요한 루트 파일들과 각종 컴파일러 옵션들을 설정하는 파일이다.
위치한 디렉토리는 TypeScript 프로젝트의 루트 디렉토리가 된다.
= Ts파일을 Js로 어떻게 컴파일할 것인가를 설정해주는 파일.
ts버전 CRA설치로 생기는 tsconfig.json의 옵션을 그대로 가져왔다.
{
"extends": "./tsconfig.path.json",
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"], // 컴파일 할 때 포함 될 라이브러리 목록
"allowJs": true, // js 파일들 ts에서 import해서 쓸 수 있는지
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"module": "esnext",
"strict": true // typescript의 type 검사 옵션 중 strict* 관련된 모든 것을 true로 만들게 된다.
},
"include": ["src", "craco.config.js"],
"exclude": ["node_modules"]
}
alias사용 이유?
상대경로로 밑에서 경로를 접근하는것보다 위에서 내려가는게 빠를 수 있다. 그리고 import 경로를 깔끔하게 만들 수 있다.
tsconfig.json에 paths 옵션을 바로 추가하면 CRA의 웹 팩 관련 문제로 옵션이 자동으로 삭제되는 현상때문에 별도의 파일로 설정해줬다.
{
// JSON의 유효성을 검사하기 위한 라이브러리
"$schema": "http://json-schema.org/draft-07/schema#",
"compilerOptions": {
"baseUrl": "./src",
"paths": {
"@atom": ["./components/atom"],
"@molecules": ["./components/molecules"],
"@organisms": ["./components/organisms"],
"@templates": ["./components/templates"],
"@hooks": ["./hooks"],
"@pages": ["./pages"],
"@utils": ["./utils"],
"@utils/*": ["./utils/*"],
"@apis": ["./apis"],
"@styles": ["./styles"],
"@styles/*": ["./styles/*"],
"@contexts": ["./contexts"],
"@contexts/*": ["./contexts/*"],
"@images": ["./images"],
"@images/*": ["./images/*"],
"@constants": ["./constants"],
"@types": ["./types"],
"@/*": ["./*"]
}
}
}
yarn add -D craco-alias
const CracoAlias = require('craco-alias');
module.exports = {
babel: {
presets: ['@emotion/babel-preset-css-prop'], //이모션의 css props문법을 이해하기 위한 프리셋
},
plugins: [
{
plugin: CracoAlias,
options: {
source: 'tsconfig', // 어떤 언어 설정을 사용할 건지
baseUrl: './src', // 루트 폴더
tsConfigPath: './tsconfig.path.json', // 경로를 설정한 파일이름
},
},
],
};
typescript-eslint
ESLint의 TypeScript 지원을 위한 패키지
@typescript-eslint/parser
로 설정@typescript-eslint
추가 plugin:@typescript-eslint/recommended
추가react/jsx-filename-extension
에 .ts
, .tsx
추가문제점
기존의 .js
와 .jsx
파일에 tsLint 규칙까지 적용되어 많은 오류가 발생했다
점진적으로 ts를 적용하기 위해선 기존의 전환 전의 js,jsx파일엔 tsLint가 적용되면 안된다. (esLint만 적용되어야 함)
해결책
eslintrc.js파일에서 rules프로퍼티에 js, jsx문법으로 인해 오류가 나는 tsLint rule들을 꺼놓고
overrides프로퍼티를 사용하여 ts, tsx파일에만 rule을 켜주어 해결했다!
rules: {
...
'@typescript-eslint/explicit-module-boundary-types': ['off'],
'@typescript-eslint/no-empty-function': ['off'],
'@typescript-eslint/no-unused-vars': ['off'],
},
overrides: [
{
files: ['*.ts', '*.tsx'],
rules: {
'@typescript-eslint/explicit-module-boundary-types': ['error'],
'@typescript-eslint/no-empty-function': ['error'],
'@typescript-eslint/no-unused-vars': ['error'],
},
},
],