tsSetting
react์ typescript๋ฅผ ์ ์ฉํ๊ณ ์ถ๋ค๋ฉด ์ฒ์๋ถํฐ ํ์ ์คํฌ๋ฆฝํธ ํ๋ก์ ํธ๋ก ์ ์ฉ์์ผ ์ฃผ๋ ๋ฐฉ๋ฒ์ด ์๊ณ , javascript๋ฅผ ์ฌ์ฉํ๋ ์ฝ๋๋ฅผ ํ์ ์คํฌ๋ฆฝํธ๋ก ์ ํ์์ผ์ฃผ๋ ๋ฐฉ๋ฒ์ด ์๋ค.
์ฒซ ๋ฒ์งธ ๋ฐฉ๋ฒ : ์ฒ์๋ถํฐ ํ์
์คํฌ๋ฆฝํธ ํ๋ก์ ํธ๋ก ๋ง๋ค๊ธฐ
npx create-react-app app-name --template typescript
์ด ๋ช ๋ น์ด๋ง ์ ๋ ฅํ๋ฉด ์๋์ผ๋ก ํ์ ์คํฌ๋ฆฝํธ ํ๊ฒฝ์ ์ธํ ํด์ค๋ค. ๋๋ฌด ์ฌ์ฐ๋ฏ๋ก js๋ฅผ ts๋ก ์ ํํ๋ ๋ ๋ฒ์งธ ๋ฐฉ๋ฒ์ผ๋ก ๋์ด๊ฐ์.
๋ ๋ฒ์งธ ๋ฐฉ๋ฒ : js๋ฆฌ์กํธ ํ๋ก์ ํธ๋ฅผ ts๋ฆฌ์กํธ ํ๋ก์ ํธ๋ก ์ ํํ๊ธฐ
๋ฆฌ์กํธ ํ๋ก์ ํธ๋ฅผ ์งํํ๋ค ๋ณด๋ฉด babel๊ณผ webpack์ด ํ์ํ ์ํฉ์ด ์ฌ ๊ฒ์ด๋ค. ๊ทธ ๋ ts-loader๋ฅผ ์ฌ์ฉํ๋ฉด ์ฝ๊ฒ ์ค์ ํ ์ ์์ผ๋ฏ๋ก ts-loader๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ธํ ํ tsํ๊ฒฝ์ ์์๋ก ๋ณด์ฌ์ฃผ๊ฒ ๋ค. ์๋๋ ํ์ฌ ์งํํ๊ณ ์๋ ํ ์ดํ๋ก์ ํธ์ ์ด๊ธฐ ์ธํ ํ์ผ์ด๋ค.
package.json
{
"name": "rochest-frontend",
"version": "0.1.0",
"dependencies": {
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"@types/jest": "^27.0.1",
"@types/node": "^14.6.1",
"@types/react": "^16.9.48",
"@types/react-dom": "^17.0.9",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"ts-loader": "^8.0.3",
"typescript": "^3.9.7",
"web-vitals": "^1.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
tsconfig.json
{
"extends": "./tsconfig-base.json",
"files": [
"src/index.tsx"
],
"compilerOptions": {
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true
},
"include": [
"src"
]
}
tsconfig-base.json
{
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
"composite": true,
"jsx": "react",
}
}
์ด๋ ๊ฒ ์ธํ ํ ํ ๊ธฐ์กด jsํ์ผ๋ค์ tsxํ์ผ๋ก ํ์ผ๋ช ๋ง ๋ฐ๊ฟ์ฃผ๊ณ , ์ถ๊ฐ์ ์ผ๋ก ๋ฐ๋ฒจ์ด๋ ์นํ์ด ํ์ํ๋ฉด ๋ฌธ์๋ฅผ ์ฝ๊ณ ์ค์ ํ์ผ๋ง ๋ฐ๊ฟ์ฃผ๋ฉด ๋ ๊ฒ์ด๋ค.
ts-loader demo github : https://github.com/TypeStrong/ts-loader/tree/3f3c0d779a9df614b978764e976d934d08f1fdca/examples/project-references-example
Last updated