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