๐
์ฌ์ด๋ ํ๋ก์ ํธ๋ฅผ ์๋ก ์์ํ๋ค.
๊ธฐ์กด์ ์ธํ ์ ๋์๋ ํ๋ก์ ํธ๋ผ, Vite + React(JavaScript) ํ๋ก์ ํธ์ TypeScript๋ฅผ ์ถ๊ฐํ๋ฉด์ ๊ฒช์๋ ๊ณผ์ ๊ณผ ํ์ํ ์ค์ ์ ์ ๋ฆฌํด๋ณด์๋ค.
1. TypeScript ์ค์น
๋จผ์ ํ๋ก์ ํธ์ TypeScript์ React ํ์ ์ ์ค์นํ๋ค.
npm install -D typescript @types/react @types/react-dom
์ค์น๊ฐ ์๋ฃ๋๋ฉด TypeScript ์ค์ ํ์ผ์ ์์ฑํ๋ค.
npx tsc --init
๊ทธ๋ฌ๋ฉด ํ๋ก์ ํธ ๋ฃจํธ์ tsconfig.json ํ์ผ์ด ์์ฑ๋๋ค.
2. tsconfig.json ์ค์
๊ธฐ๋ณธ ์์ฑ๋ tsconfig.json์ Vite ํ๊ฒฝ์ ๋ง๊ฒ ์ผ๋ถ ์์ ํ๋ค.
{
"compilerOptions": {
"target": "ESNext",
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"module": "ESNext",
"moduleResolution": "Bundler",
"jsx": "react-jsx",
"strict": true,
"skipLibCheck": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"allowJs": true
},
"include": ["src"]
}
โ ์ฃผ์ ์ต์
- strict
TypeScript์ ํ์ ๊ฒ์ฌ๋ฅผ ์๊ฒฉํ๊ฒ ํ๋ค.
- allowJs
๊ธฐ์กด JavaScript ํ์ผ์ ๊ทธ๋๋ก ์ฌ์ฉํ ์ ์๊ฒ ํด์ค๋ค.
๊ธฐ์กด ํ๋ก์ ํธ์ TypeScript๋ฅผ ๋์
ํ ๋ ์ ์ฉํจ. ํ์ผ์ด ๋ง์ ๋๋ ์ค์!
- moduleResolution: Bundler
Vite ํ๊ฒฝ์ ๋ง๋ ๋ชจ๋ ํด์ ๋ฐฉ์
3. vite-env.d.ts ์์ฑ
Vite ํ๋ก์ ํธ์์๋ ํ๊ฒฝ ๋ณ์ ํ์
์ ์ํด
vite-env.d.ts ํ์ผ์ด ํ์ํ๋ค.
src ํด๋ ์์ ์๋ ํ์ผ์ ์์ฑํ๊ณ ๋ด์ฉ์ ๋ค์๊ณผ ๊ฐ๋ค.
/// <reference types="vite/client" />
์ด ํ์ผ์ด ์์ด์ผ import.meta.env.VITE_API_URL ๊ฐ์ ์ฝ๋์์ ํ์ ์ด ์ ์์ ์ผ๋ก ์๋ํ๋ค.
4. vite.config.js → vite.config.ts ๋ณ๊ฒฝ
Vite ์ค์ ํ์ผ๋ TypeScript๋ก ๋ณ๊ฒฝํ ์ ์๋ค.
vite.config.js → vite.config.ts
5. main ํ์ผ ๋จผ์ TypeScript๋ก ๋ณํ
๊ธฐ์กด ํ๋ก์ ํธ๋ฅผ TypeScript๋ก ๋ง์ด๊ทธ๋ ์ด์ ํ ๋๋ entry ํ์ผ๋ถํฐ ๋ณํํ๋ ๊ฒ์ด ์ข๋ค๊ณ ํ๋ค.
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
์ฌ๊ธฐ์ !๋ Non-null assertion์ผ๋ก
root ์์๊ฐ null์ด ์๋๋ผ๋ ๊ฒ์ TypeScript์๊ฒ ์๋ ค์ฃผ๋ ์ญํ ์ ํ๋ค.
main ์ดํ๋ก๋ App ์ปดํฌ๋ํธ, ๋ค๋ฅธ ์ปดํฌ๋ํธ๋ค์ ํ๋์ฉ ๋ณํํด์ฃผ๋ฉด ๋๋ค.
๊ธฐ์กด ํ๋ก์ ํธ๋ฅผ TypeScript๋ก ๋ง์ด๊ทธ๋ ์ด์ ํ ๋๋ ๋ณดํต ์ด๋ฌํ ์์๋ก ์งํํ๋ ๊ฒ์ด ์ข๋ค๊ณ .. (์๋ฌด๋๋)
๐ก ํ์ ์ ์
1. Props ํ์ ์ ์
์ปดํฌ๋ํธ๋ฅผ TypeScript๋ก ๋ณํํ ๋๋ Props ํ์ ์ ์ ์ํด์ค๋ค.
type ButtonProps = {
children: React.ReactNode;
onClick?: () => void;
};
export function Button({ children, onClick }: ButtonProps) {
return <button onClick={onClick}>{children}</button>;
}
์ด๋ ๊ฒ ํ๋ฉด ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ ๋ ํ์ ์ฒดํฌ๊ฐ ๊ฐ๋ฅํด์ง๋ค.
2. API ํ์ ์ ์
API ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ ๋๋ ํ์ ์ ์ ์ํด์ค๋ค.
interface User {
id: number;
name: string;
}
axios ์์ฒญ์์๋ ํ์ ์ ์ง์ ํ ์ ์๋ค.
axios.get<User[]>("/api/users");
์ค๋ฌด์์๋ ์์ฒ๋ผ ์ ์ํ ํ์ ์ ๋ณ๋ ํด๋๋ก ๊ด๋ฆฌํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง๋ค.
src
โ components
โ hooks
โ api
โ types
โ utils
โ pages
src/types/user.ts
export interface User {
id: number;
name: string;
}
๋ง๋ฌด๋ฆฌ
๊ธฐ์กด Vite + React ํ๋ก์ ํธ์ TypeScript๋ฅผ ์ ์ฉํ ๋ ํ์ํ ๊ณผ์
- TypeScript ์ค์น
- tsconfig.json ์์ฑ ๋ฐ ์ค์
- vite-env.d.ts ์์ฑ
- vite.config.ts ๋ณํ
- main.tsx๋ถํฐ ๋ณํ
- ์ปดํฌ๋ํธ๋ฅผ ํ๋์ฉ TypeScript๋ก ๋ณํ
- Props์ API ํ์ ์ ์
์ฒ์์๋ ๋ฒ๊ฑฐ๋กญ๊ฒ ๋๊ปด์ง ์ ์์ง๋ง
ํ๋ก์ ํธ ๊ท๋ชจ๊ฐ ์ปค์ง์๋ก TypeScript์ ์ฅ์ ์ด ํฌ๊ฒ ๋๊ปด์ง๋ค.
์์ผ๋ก๋ ์ ํ๋ก์ ํธ๋ฅผ ๋ง๋ค ๋
์ฒ์๋ถํฐ React + TypeScript๋ก ์์ํ๋ ๊ฒ์ด ๋ ์ข์ ์ ํ์ผ ๊ฒ ๊ฐ๋ค.

'Dev > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| "๋ฆฌ์กํธ๋ ๊ฐ์ DOM์ ์ฌ์ฉํ์ฌ ์ฑ๋ฅ์ ์ต์ ํํฉ๋๋ค." ๊ฐ ๋ฌด์จ ๋ง์ด์ผ? (0) | 2023.09.13 |
|---|---|
| ๋ฆฌ์กํธ ์ฐ๋จน 3 - Component ์ปดํฌ๋ํธ (1) | 2023.04.21 |
| ๋ฆฌ์กํธ ์ฐ๋จน 2 (1) | 2023.04.14 |
| ๋ฆฌ์กํธ ์ฐ๋จน 1 (1) | 2023.04.13 |
๋๊ธ