[React.ts] React๋ฅผ Typescript๋กœ ์‹œ์ž‘ํ•ด๋ณด๊ธฐ - ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ๊ตฌ์„ฑ

๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š”. Typescript๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ฒซ ๊ธ€์ž…๋‹ˆ๋‹ค. 

Typescript์˜ ์ฒซ ์‹œ์ž‘์„ React ํ”„๋กœ์ ํŠธ๋กœ ์‹œ์ž‘ํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์ œ๊ฐ€ ์ด๋ฒˆ์— ๋ฉ”์ธ ํ™ˆํŽ˜์ด์ง€๋ฅผ ๋‹ค์‹œ ๋ฆฌ๋‰ด์–ผ ํ•˜๊ฒŒ ๋˜๋ฉด์„œ ์ง€๋‚œ 2๋…„ ๋™์•ˆ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜๋˜ React๋ฅผ ๋‹ค์‹œ ์‹œ์ž‘ํ•˜๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ์š”. ๋น„๋ก ๋งŒ๋“œ๋Š” ์ž ๊น ๋™์•ˆ์ด์—ˆ์ง€๋งŒ ๋ฐฐ์›€๊ณผ ๊ฐ™์ด ๋”๋ถˆ์–ด ์ง„ํ–‰ํ•˜๊ฒŒ ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— React์˜ ๋ณ€๊ฒฝ์ ๋„ ๊ฐ™์ด ์‚ดํŽด๋ด„๊ณผ ๋”๋ถˆ์–ด์„œ ์ด๋ฒˆ์—๋Š” Typescript๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐœ๋ฐœํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 

Create react app

React๋ฅผ ๋น ๋ฅด๊ฒŒ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ create-react-app์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. Typescript๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋„ ์—ญ์‹œ ์ด ๋ช…๋ น์–ด๋กœ ์‰ฝ๊ณ  ํ•œ ๋ฒˆ์— ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

$ create-react-app [app-name] --template typescript

์ผ๋ฐ˜์ ์œผ๋กœ create-react-app์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋˜๋ฉด Javascript๋ฅผ Base๋กœ ์ƒ์„ฑ์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— --typescript ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•˜์—ฌ Typescript๋กœ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

Javascript๋ž‘ ์–ด๋–ค ๋ถ€๋ถ„์ด ๋‹ค๋ฅธ์ง€ ๊ฐ„๋‹จํžˆ ์‚ดํŽด๋ณด๋„๋ก ํ•˜์ฃ .

 

Typescript React

React๋ฅผ Typescript๋กœ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ Typescript ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋˜, Javascript์˜ ๊ธฐ๋ณธ React ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ตฌ์กฐ์—์„œ Typescript configuration ํŒŒ์ผ์ด ํ•œ ๊ฐœ ๋” ์ถ”๊ฐ€๋˜์–ด ๊ตฌ์„ฑ๋˜๊ณ , package.json์—๋Š” Typescript๋กœ ์ •์˜๋œ ํŒจํ‚ค์ง€๋Š” @types๋ผ๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง€๊ณ  ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 

.
โ”œโ”€โ”€ node_modules
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ public
โ”‚   โ”œโ”€โ”€ favicon.ico
โ”‚   โ”œโ”€โ”€ index.html
โ”‚   โ”œโ”€โ”€ logo192.png
โ”‚   โ”œโ”€โ”€ logo512.png
โ”‚   โ”œโ”€โ”€ manifest.json
โ”‚   โ””โ”€โ”€ robots.txt
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ src
โ”‚   โ”œโ”€โ”€ App.css
โ”‚   โ”œโ”€โ”€ App.test.tsx
โ”‚   โ”œโ”€โ”€ App.tsx
โ”‚   โ”œโ”€โ”€ index.css
โ”‚   โ”œโ”€โ”€ index.tsx
โ”‚   โ”œโ”€โ”€ logo.svg
โ”‚   โ”œโ”€โ”€ react-app-env.d.ts
โ”‚   โ””โ”€โ”€ serviceWorker.ts
โ”œโ”€โ”€ tsconfig.json
โ””โ”€โ”€ yarn.lock

Typescript ํ™˜๊ฒฝ์—์„œ ์กด์žฌํ•˜๋Š” tsconfig.json์€ Typescript ์–ธ์–ด์˜ ํ™˜๊ฒฝ ์„ค์ • ํŒŒ์ผ์ž…๋‹ˆ๋‹ค. Typescript์˜ ๋นŒ๋“œ ํ™˜๊ฒฝ์„ ๋‹ค์–‘ํ•˜๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

{
  "name": "practice-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@types/jest": "24.0.21",
    "@types/node": "12.12.5",
    "@types/react": "16.9.11",
    "@types/react-dom": "16.9.3",
    "react": "^16.11.0",
    "react-dom": "^16.11.0",
    "react-scripts": "3.2.0",
    "typescript": "3.6.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

package.json์„ ์‚ดํŽด๋ณด๋ฉด, ๋ˆˆ์— ๋„๋Š” ๊ฒƒ์ด ๋ฐ”๋กœ @types๋กœ ์‹œ์ž‘ํ•˜๋Š” ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. ์ด ํŒจํ‚ค์ง€๋Š” Typescript ์–ธ์–ด์—์„œ ํ•ด๋‹น ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ํ™•์žฅ ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค.

Javascript๋กœ ๊ฐœ๋ฐœ๋œ ํŒจํ‚ค์ง€๋Š” Typescript์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜๋Š” ์žˆ์ง€๋งŒ Typescript๋กœ declare ๋˜์–ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ธฐ์กด์˜ ํŒจํ‚ค์ง€๋“ค์€ Javascript๋กœ export ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— Typescript์—์„œ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ฐ˜๋“œ์‹œ Typescript์—์„œ declare ํ•ด์ค˜์•ผ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ •์˜ํ•œ ๊ฒƒ์ด ๋ฐ”๋กœ @types/[package-name] ์ž…๋‹ˆ๋‹ค.

์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์€ ๊ทธ๋Ÿฌํ•œ ํŒจํ‚ค์ง€๊ฐ€ ์žˆ๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์€ ํŒจํ‚ค์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ…Œ๋ฉด jest, react ๋“ฑ์˜ ๊ธฐ๋ณธ ํŒจํ‚ค์ง€๋Š” ๋ณ„๋„๋กœ ๊ตฌ์„ฑ์ด ๋˜์–ด ์žˆ์ง€๋งŒ MobX ํŒจํ‚ค์ง€์˜ ๊ฒฝ์šฐ, ๊ธฐ์กด Javascript ํŒจํ‚ค์ง€์—์„œ ๊ทธ๋Œ€๋กœ Typescript์—์„œ๋„ declare๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— MobX์˜ ๊ฒฝ์šฐ, ๋ณ„๋„์˜ @types ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

๋งŒ์•ฝ Typescript๋กœ declare๋˜์ง€ ์•Š์€ ํŒจํ‚ค์ง€๋ฅผ Typescript์—์„œ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, ์•„๋ž˜์˜ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค.

๋ฐ˜์‘ํ˜•

'Programming > React' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[React.ts] React๋ฅผ Typescript๋กœ ์‹œ์ž‘ํ•ด๋ณด๊ธฐ - Props, State  (0) 2019.11.02
TAGS.

Tistory Comments