1. Node.js & TypeScript ํ๊ฒฝ ๊ตฌ์ถ ๋ฐ ์ค์น (Installation)
๐ป ์์คํ
์ค์น ๋ฐ ์คํ ์๊ตฌ์ฌ์ (System Requirements)
| ํญ๋ชฉ | ์ต์ ์ฌ์ (Minimum) | ๊ถ์ฅ ์ฌ์ (Recommended) | ์ฐธ๊ณ ๋ฐ ๋น๊ณ |
|---|
| ์ด์์ฒด์ (OS) | Linux, macOS, Windows 10/11 | Ubuntu 22.04 LTS, macOS (Apple Silicon), Windows 11 | 64-bit OS ํ๊ฒฝ ๊ถ์ฅ |
| CPU | 1 ์ฝ์ด ์ด์ | 2 ์ฝ์ด ~ 4 ์ฝ์ด ์ด์ | TypeScript ์ปดํ์ผ๋ฌ(tsc) ํ์
์ฒดํฌ ๋ฐ ๋ฒ๋ค๋ง |
| ๋ฉ๋ชจ๋ฆฌ (RAM) | 1 GB ์ด์ | 2 GB ~ 4 GB ์ด์ | ๋๊ท๋ชจ ํ๋ก์ ํธ tsc ์ฆ๋ถ ๋น๋ ๋ฐ ESLint ๊ตฌ๋ ์ |
| ๋์คํฌ (Storage) | 1 GB ์ด์์ ์ฌ์ ๊ณต๊ฐ | 5 GB ~ 10 GB ์ด์์ SSD | Node.js ๋ฐํ์, node_modules ์บ์ ๋ฐ ํจํค์ง |
| ๋คํธ์ํฌ / ๋ฐํ์ | Node.js 18, 20, 22 LTS, npm / pnpm / yarn | TypeScript 5.x (npm install -g typescript) | nvm / fnm ๋ฒ์ ๊ด๋ฆฌ์ ๊ถ์ฅ |
# โก [1] Node.js ๋ฒ์ ๊ด๋ฆฌ์ ์ค์น (nvm ๋๋ fnm ๊ถ์ฅ)
# Linux / macOS (fnm ์ด๊ณ ์ ๋งค๋์ )
curl -fsSL https://fnm.vercel.app/install | bash
fnm install --lts
fnm use --lts
node -v && npm -v
# Windows (winget)
winget install Schniz.fnm
# ๋๋ ๊ณต์ LTS ์ธ์คํจ๋ฌ: winget install OpenJS.NodeJS.LTS
# ๐ท [2] TypeScript ํ๋ก์ ํธ ์ด๊ธฐํ & ๊ฐ๋ฐ ํ๊ฒฝ ๊ตฌ์ฑ
# ํ๋ก์ ํธ ํด๋ ์์ฑ ๋ฐ package.json ์ด๊ธฐํ
mkdir my-ts-project && cd my-ts-project
npm init -y
# TypeScript ๋ฐ ํ์
์ ์, ์ด๊ณ ์ ์คํ ๋ฌ๋(tsx) ์ค์น
npm install -D typescript @types/node tsx
# tsconfig.json ์์ฑ
npx tsc --init
// โ๏ธ [3] ์ถ์ฒ tsconfig.json ๊ธฐ๋ณธ ์ค์
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "./dist"
},
"include": ["src/**/*"]
}
# ๐ [4] TypeScript ํ์ผ ์ปดํ์ผ ์์ด ์ฆ์ ์คํ
npx tsx src/index.ts
# ๋น๋ ์คํ
npx tsc
2. ๋ฐฐ์ด ๊ณ ๊ธ ๋ค๋ฃจ๊ธฐ (Array Methods)
// ์ค๋ณต ์ ๊ฑฐ (Set ํ์ฉ)
const uniqueList = [...new Set([1, 2, 2, 3, 4, 4])];
// ํค ๊ธฐ์ค์ผ๋ก ๊ฐ์ฒด ๋ฐฐ์ด ๊ทธ๋ฃนํ (ES2024 Object.groupBy)
const grouped = Object.groupBy(items, item => item.category);
// ํํํ ๋ฐ ๋งคํ (flatMap)
const tags = posts.flatMap(post => post.tags);
// ํน์ ์กฐ๊ฑด ๋ง์กฑ ์ฌ๋ถ ๊ฒ์ฌ
const hasAdmin = users.some(u => u.role === 'admin');
const allVerified = users.every(u => u.isVerified);
3. ๊ฐ์ฒด ๊ตฌ์กฐ๋ถํด & ๋ ๋ณํฉ
// ๊ธฐ๋ณธ๊ฐ ์ง์ ๋ฐ ์ด๋ฆ ๋ณ๊ฒฝ ๊ตฌ์กฐ๋ถํด
const { name: userName, age = 20 } = user;
// ์ต์
๋ ์ฒด์ด๋ & ๋ ๋ณํฉ ์ฐ์ฐ์ (?? vs ||)
// ?? ๋ null ๋๋ undefined ์ผ ๋๋ง ๊ธฐ๋ณธ๊ฐ์ ์ฌ์ฉ (0, '' ์ ์ ์ง)
const port = process.env.PORT ?? 3000;
const street = user?.address?.street ?? '์ฃผ์ ๋ฏธ์
๋ ฅ';
// ๊ฐ์ฒด ๋์ ํค ์ถ๊ฐ
const dynamicObj = {
[dynamicKey]: 'value',
...(isAdmin && { permissions: ['ALL'] }),
};
4. ๋น๋๊ธฐ ์ ์ด & Promise ๋ณ๋ ฌ ์ฒ๋ฆฌ
// ์ฌ๋ฌ ๋น๋๊ธฐ ์์
๋์ ๋ณ๋ ฌ ์คํ (ํ๋๋ผ๋ ์คํจํ๋ฉด ์ค๋จ)
const [users, posts] = await Promise.all([fetchUsers(), fetchPosts()]);
// ๋ชจ๋ ์์
์ ์ฑ๊ณต/์คํจ ์ฌ๋ถ๋ฅผ ๋๊น์ง ์ง๊ณ
const results = await Promise.allSettled(urls.map(fetchData));
results.forEach(res => {
if (res.status === 'fulfilled') console.log(res.value);
else console.error(res.reason);
});
// ํ์์์์ ์ง์ํ๋ ๋น๋๊ธฐ ๋ํผ
const timeout = (ms: number) => new Promise((_, reject) =>
setTimeout(() => reject(new Error('Timeout!')), ms)
);
await Promise.race([fetchData(), timeout(5000)]);
5. TypeScript ํ์ ์ ํธ๋ฆฌํฐ ํ์
interface User {
id: string;
name: string;
email: string;
avatarUrl?: string;
}
// ๋ชจ๋ ์์ฑ์ ์ ํ(optional)์ผ๋ก ๋ง๋ค๊ธฐ
type PartialUser = Partial<User>;
// ํน์ ์์ฑ๋ง ๊ณจ๋ผ๋ด๊ธฐ
type UserPreview = Pick<User, 'id' | 'name'>;
// ํน์ ์์ฑ๋ง ์ ์ธํ๊ธฐ
type UserWithoutId = Omit<User, 'id'>;
// ๋ชจ๋ ์์ฑ์ ์ฝ๊ธฐ ์ ์ฉ์ผ๋ก ๋ง๋ค๊ธฐ
type ReadonlyUser = Readonly<User>;
// Record ํ์
(ํค์ ๊ฐ ํ์
๋งตํ)
type RoleMap = Record<string, string[]>;
6. ํ์
๊ฐ๋ (Type Narrowing)
// ์ฌ์ฉ์ ์ ์ ํ์
๊ฐ๋ (is ์ฐ์ฐ์)
function isError(value: unknown): value is Error {
return value instanceof Error;
}
// ์๋ณ ๊ฐ๋ฅํ ์ ๋์จ (Discriminated Union)
type ApiResponse =
| { status: 'success'; data: User }
| { status: 'error'; message: string };
function handleResponse(res: ApiResponse) {
if (res.status === 'success') {
console.log(res.data.name); // ํ์
์์ ๋ณด์ฅ
} else {
console.error(res.message);
}
}
7. ํด๋ก์ & ๋๋ฐ์ด์ค ์ ํธ๋ฆฌํฐ
// ๊ฒ์์ฐฝ ์
๋ ฅ ์ง์ฐ ์ฒ๋ฆฌ์ฉ ๋๋ฐ์ด์ค(Debounce)
function debounce<T extends (...args: any[]) => any>(
fn: T,
delay: number
) {
let timer: NodeJS.Timeout;
return (...args: Parameters<T>) => {
clearTimeout(timer);
timer = setTimeout(() => fn(...args), delay);
};
}
8. ์ค๋ฌด TypeScript API ์๋ฒ ๊ตฌํ ์์
// src/server.ts - Express + TypeScript ์๋ํฌ์ธํธ ๊ตฌํ
import express, { Request, Response } from 'express';
const app = express();
app.use(express.json());
interface CreateProductDto {
name: string;
price: number;
}
app.post('/api/products', (req: Request<{}, {}, CreateProductDto>, res: Response) => {
const { name, price } = req.body;
if (!name || price <= 0) {
return res.status(400).json({ error: '์ ํจํ์ง ์์ ์์ฒญ ๋ฐ์ดํฐ์
๋๋ค.' });
}
const newProduct = { id: crypto.randomUUID(), name, price };
return res.status(201).json(newProduct);
});
const PORT = process.env.PORT ?? 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
์๊ฒฌ ๋ฐ ์ง๋ฌธ
0์์ง ๋ฑ๋ก๋ ์๊ฒฌ์ด ์์ต๋๋ค. ์ฒซ ๋ฒ์งธ ๋๊ธ์ ๋จ๊ฒจ๋ณด์ธ์!
๋๊ธ ์์
๋๊ธ ์ญ์