๐Ÿ“œ

JavaScript & TypeScript ์น˜ํŠธ์‹œํŠธ

Node.js/TS ํ™˜๊ฒฝ ์„ค์น˜ ๋ฐ tsconfig, ๋ฐฐ์—ด/๊ฐ์ฒด ์กฐ์ž‘, ๋น„๋™๊ธฐ Promise/Async ํŒจํ„ด, TypeScript ํ•ต์‹ฌ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํƒ€์ž…, ํƒ€์ž… ๊ฐ€๋“œ ๋ฐ Express ์„œ๋ฒ„ ์‹ค๋ฌด ์˜ˆ์ œ

--views
๋‹จ์ˆ˜:
#javascript#typescript#node#npm#es6#frontend#backend#web#install์ตœ์ข… ์—…๋ฐ์ดํŠธ: 2026-10-04

1. Node.js & TypeScript ํ™˜๊ฒฝ ๊ตฌ์ถ• ๋ฐ ์„ค์น˜ (Installation)

๐Ÿ’ป ์‹œ์Šคํ…œ ์„ค์น˜ ๋ฐ ์‹คํ–‰ ์š”๊ตฌ์‚ฌ์–‘ (System Requirements)

ํ•ญ๋ชฉ์ตœ์†Œ ์‚ฌ์–‘ (Minimum)๊ถŒ์žฅ ์‚ฌ์–‘ (Recommended)์ฐธ๊ณ  ๋ฐ ๋น„๊ณ 
์šด์˜์ฒด์ œ (OS)Linux, macOS, Windows 10/11Ubuntu 22.04 LTS, macOS (Apple Silicon), Windows 1164-bit OS ํ™˜๊ฒฝ ๊ถŒ์žฅ
CPU1 ์ฝ”์–ด ์ด์ƒ2 ์ฝ”์–ด ~ 4 ์ฝ”์–ด ์ด์ƒTypeScript ์ปดํŒŒ์ผ๋Ÿฌ(tsc) ํƒ€์ž… ์ฒดํฌ ๋ฐ ๋ฒˆ๋“ค๋ง
๋ฉ”๋ชจ๋ฆฌ (RAM)1 GB ์ด์ƒ2 GB ~ 4 GB ์ด์ƒ๋Œ€๊ทœ๋ชจ ํ”„๋กœ์ ํŠธ tsc ์ฆ๋ถ„ ๋นŒ๋“œ ๋ฐ ESLint ๊ตฌ๋™ ์‹œ
๋””์Šคํฌ (Storage)1 GB ์ด์ƒ์˜ ์—ฌ์œ  ๊ณต๊ฐ„5 GB ~ 10 GB ์ด์ƒ์˜ SSDNode.js ๋Ÿฐํƒ€์ž„, node_modules ์บ์‹œ ๋ฐ ํŒจํ‚ค์ง€
๋„คํŠธ์›Œํฌ / ๋Ÿฐํƒ€์ž„Node.js 18, 20, 22 LTS, npm / pnpm / yarnTypeScript 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
0 / 1500