2025. 7. 3. 19:19ใFront-end/TypeScript
[ ๋ํ ์ด๋ฏธ์ง ]
๋ชฉ์ฐจ
1. ํ๋ก์ ํธ์ ts ์ค์น ๋ฐ ์ด๊ธฐํ
2. ํ์คํ ํ๋ก์ ํธ ์ค์ ํ๊ธฐ
3. ํ ์คํธ ์ค์ ํ๊ธฐ
1. ํ๋ก์ ํธ์ ts ์ค์น ๋ฐ ์ด๊ธฐํ
ํ์ต์ฉ ํ๋ก์ ํธ ํด๋์ ํ์ ์คํฌ๋ฆฝํธ๋ฅผ ์ค์นํ๊ณ ์ด๊ธฐํํด๋ณด์.
์ค์น
npm install -D typescript

** ๊ทธ๋์ --save-dev(์ฝ์นญ : -D)์ ์๋ฏธ๋ฅผ ์ ๋ชฐ๋๋๋ฐ ์ด๋ฒ์ ์ง๊ณ ๋์ด๊ฐ๋ดค๋ค.
์ด๊ฑด ์ค์นํ๋ ํจํค์ง๋ฅผ ๊ฐ๋ฐ(Development) ์์กด์ฑ์ผ๋ก ์ถ๊ฐํ๋ค๋ ์๋ฏธ๋ก,
ํ๋ก์ ํธ๋ฅผ NPM ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ๋ฐฐํฌํ ๋ ํ์ ์คํฌ๋ฆฝํธ๋ฅผ ํฌํจํ์ง ์๋๋ค๋ ์๋ฏธ์ด๋ค.
- ๊ฐ๋ฐํ ๋๋ง ํ์ํ ํด(๋ฐํ์ ์ ํ์ ์์)
+ ์ต๊ทผ npm ๋ฒ์ (6 ์ด์)์์๋ ์ ํค์๋๋ ์๋ต ๊ฐ๋ฅํ ๊ธฐ๋ณธ๊ฐ์ด๋ผ๊ณ ํ๋ค.
์ด๊ธฐํ
npx tsc --init
์์ ๊ฐ์ด ์ด๊ธฐํ๋ฅผ ํ๋ฉด tsconfig.json ํ์ผ์ด ์ถ๊ฐ๋ก ์์ฑ๋๋ค.
tsconfig.json ํ์ผ์ ๋ณด๋ฉด ์ฃผ์์ด ์ฐธ ๋ง์๋ฐ ์ฃผ์๋ค์ ์ ์ธํ๋ฉด ์๋์ ๊ฐ์ด ๋จ๋๋ค.
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
- "target": "es2016"
ํ๋ก์ ํธ์ ํ์ ์คํฌ๋ฆฝํธ ํ์ผ์ ECMAScript 2016 ๋ฌธ๋ฒ์ผ๋ก ์ปดํ์ผํ๋ค. - "module": "commonjs"
์ปดํ์ผ๋ JS ์ฝ๋์์ ์ด๋ค ๋ชจ๋ ์์คํ ์ ์ฌ์ฉํ ์ง ์ ์ํ๋ ์ต์ ์ผ๋ก, "commonjs" ์ต์ ์ ์ฌ์ฉํ๋ฉด ECMAScript ๋ชจ๋ ๋ฌธ๋ฒ์ ์ฌ์ฉํ ์ ์๋ค. next.js๊ฐ์ ํ๋ ์์ํฌ๋ฅผ ์ฌ์ฉํ๋ฉด "commonjs"๊ฐ ์๋ "esnext"๋ฅผ ์ฌ์ฉํ๊ฒ ๋๋ค. - "esModuleInterop": true
ECMAScript ๋ชจ๋ ์ธ์ ์ํฌํธ๋ ๋ชจ๋๋ค์ ํ์ค์ ์ผ์น์ํจ๋ค. - "forceConsistentCasingInFileNames": true
๋์๋ฌธ์๋ฅผ ๊ตฌ๋ณํ๋ ํ์ผ ์์คํ ์ง์ ์ต์ ์ด๋ค. - "strict": true
ํ์ ์์ ์ฑ์ ๊ฐ๋ ฅํ๊ฒ ์ ์ฉํ๋ผ๋ ์ค์ ์ด๋ค. (ํ์ ๋ช ์, ํ์ ์ถ๋ก ์ด any์ผ ๊ฒฝ์ฐ ์๋ฌ, ํ๋กํผํฐ๋ฅผ ๋ฐ๋์ ์ด๊ธฐํ ๋ฑ๋ฑ) - "skipLibCheck": true
์ค๋ฅ๊ฐ ์๋ ํ์ ์ ์ ํ์ผ๋ค์ ์ค์นํ๋ค๊ณ ๊ฐ์ ํ์ฌ, ์ปดํ์ผ๋ฌ๊ฐ ํ์ ์ ์ ํ์ผ์ ๋ค์ ๊ฒ์ฌํ์ง ์๋๋ค. -> ์ปดํ์ผ๋ฌ ๋์ ์๋๊ฐ ์กฐ๊ธ ๋นจ๋ผ์ง๋ค.
์๋์ ๊ฐ์ ์ค์ ์ compilerOptions์ ์ถ๊ฐํด๋ณด์
"rootDir": "./src", // src ํด๋๋ฅผ ๋ฃจํธ ๊ฒฝ๋ก๋ก ์ง์
"outDir": "./dist" // ์ปดํ์ผ๋ ํ์ผ์ dist ํด๋์ ์ ์ฅํ๋๋ก ์ง์
์ด์ src ํด๋์ index.ts๋ฅผ ์์ฑํ๊ณ ์ปดํ์ผ ํ๋ฉด dist ํด๋์์ ์ปดํ์ผ๋ ํ์ผ์ ํ์ธํ ์ ์๋ค.
// src/index.ts
console.log("Hello World!");
์ปดํ์ผ
npx tsc
// dist/index.ts
"use strict";
console.log("Hello, TypeScript!");
2. ํ์คํ ํ๋ก์ ํธ ์ค์ ํ๊ธฐ
ํ๋ก ํธ์ฉ, ๋ฐฑ์ฉ tsconfig ํ์ผ์ ๊ฐ๊ฐ ๋ง๋ค๊ณ composite์ผ๋ก ๊ณต์ ๋ ์์กด์ฑ์ ๋ถ๋ฌ์๋ณด์.
Node.js์ ๋ธ๋ผ์ฐ์ ๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์คํํ์ง๋ง, ์ํํ๋ ์ญํ ์ด ๋ง์ด ๋ค๋ฅด๋ค.
๋ธ๋ผ์ฐ์ ๋ฐ์์ JavaScript๋ฅผ ์คํํ ์ ์๋๋ก ๋ง๋ ํ๊ฒฝ(runtime)์ธ Node.js ์ฝ๋๋ฅผ ์์๋ก ๋ณด์.
const http = require('http');
const hostname = '127.0.0.1';
const port = process.env.PORT || 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
์ด์ด์ ํ๋ฉด์ ์น์ฌ์ดํธ๋ฅผ ๊ทธ๋ ค์ฃผ๋ ํ๊ฒฝ์ธ ๋ธ๋ผ์ฐ์ ์ฝ๋๋ฅผ ๋ณด์.
import {msg} from `./msg.js`;
document.querySelector('button')?.addEventListener("click", () => {
console.log(msg);
});
๋๋ค ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๋ก ์์ฑ๋์ง๋ง ๋ค๋ฅธ ์ ์ด ๋ง์, ํ ํ์ ์คํฌ๋ฆฝํธ ํ๋ก์ ํธ๋ก ์ง์ํ๊ธฐ๋ ์ด๋ ต๋ค.
๋ฐ๋ผ์ ํ์คํ ์ ํ๋ฆฌ์ผ์ด์ ๊ตฌํ์ ์ํด์๋ ๊ฐ ์คํ์ ๋ชจ๋ ์ฒ๋ฆฌํ๋ ๋ ๊ฐ์ง ํ์ ์คํฌ๋ฆฝํธ ์ค์ ํ์ผ์ ๋ง๋ค์ด์ผ ํ๋ค.
๋ฐฑ์๋์ ๊ตฌํ๊ณผ ์ค์ ์ server๋ผ๋ ์ ํด๋๋ฅผ ๋ง๋ค์ด์ ์์ฑํด๋ณผ ์ ์๋ค.
์๋ฒ๋ฅผ ๋ง๋ค๊ธฐ ์ํ ํ๋ ์์ํฌ๋ฅผ ์ค์นํ๊ณ ์๋ฒ์ฉ tsconfig.json ํ์ผ ๋ด์ฉ์ ํ์ธํด๋ณด์.
npm install -D express
{
"compilerOptions": {
"target": "ESNext",
"lib": ["ESNext"],
"module": "commonjs",
"rootDir": "./",
"outDir": "../dist/server",
"moduleResolution": "node",
"types": ["node"],
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
์ด๋ node๋ฅผ ์ฐพ์ ์ ์๋ค๋ ์๋ฌ๊ฐ ๋ฐ ์ ์๋๋ฐ, @types/node๋ฅผ ์ค์นํด์ ํด๊ฒฐํด์ฃผ๋ฉด ๋๋ค
https://ldd6cr-adness.tistory.com/325
@types : JS ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ TS ํ๋ก์ ํธ์์ ์ฌ์ฉํ๊ธฐ
๋ชฉ์ฐจ1. DefinitelyTyped๋?1. DefinitelyTyped๋?ํ์ ์คํฌ๋ฆฝํธ์์ ์๋ฐ์คํฌ๋ฆฝํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํ ํ์ ์ ์ธ ํ์ผ(.d.ts)์ ๋ชจ์๋ ์คํ์์ค ์ ์ฅ์์ด๋ค. https://github.com/DefinitelyTyped/DefinitelyTyped G
ldd6cr-adness.tistory.com
server ํด๋์ ์ด์ด์ ๊ฐ๋จํ index.ts ํ์ผ์ ์์ฑํ๊ณ ์คํํด์ฃผ๋ฉด dist์ ์คํํ์ผ์ด ์์ฑ๋ ๊ฑธ ํ์ธํ ์ ์๋ค.
// server/index.ts
console.log('Server running...');
์คํ
npx tsc -p server/tsconfig.json

ํ๋ก ํธ์ ๋ฐฑ์ tsconfig.json์ ๊ฐ๊ฐ ์์ฑํด๋ดค๋๋ฐ, ์์์๋ ๊ฐ ํด๋์ tsconfig.json์ด๋ผ๋ ์ด๋ฆ์ผ๋ก ํ์ผ์ ์์ฑํ์ง๋ง,
๋๋ค ๋ฃจํธ์ ์์ฑํ๋ ๋์ ์ด๋ฆ์ tsconfig.server.json ๊ณผ tsconfig.client.json๊ณผ ๊ฐ์ด ๋ฐ๊ฟ์ ์ ์ฅํ ์๋ ์๋ค.
ํน์ ๋ ์ค์ ํ์ผ์ด ์์กด์ฑ์ ๊ณต์ ํด์ผํ ์๋ ์๋ค.
์ด ๋๋ client, server ํด๋์ ๊ฐ์ ์์น์ shared ํด๋๋ฅผ ์์ฑํ๊ณ ๊ทธ ์์ tsconfig.json์ ์์ฑํด๋ณผ ์ ์๋ค.
// shared/tsconfig.json
{
"compilerOptions": {
"composite": true,
"target": "ESNext",
"module": "ESNext",
"rootDir": "../shared/",
"outDir": "../dist/shared",
"moduleResolution": "Node",
"types": [],
"declaration": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
}
- "composite": true => ๋ค๋ฅธ ํ๋ก์ ํธ๊ฐ ์ด ํ๋ก์ ํธ๋ฅผ ์ฐธ์กฐํ ์ ์๋ค
- "declaration": true => ๋ค๋ฅธ ํ๋ก์ ํธ๊ฐ ํ์ ์ ๋ณด๋ฅผ ๋ถ๋ฌ์ฌ ์ ์๋๋ก ์ฝ๋์์ d.ts ํ์ผ์ ์์ฑํ๋ค.
์๋ฒ์ ํด๋ผ์ด์ธํธ์ ์ค์ ํ์ผ์ ์๋ ํ์ ์ถ๊ฐํ์ฌ ๊ณต์ ๋ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋ค.
{
"compilerOptions": {
// ...
},
"references": [
{
"path": "../shared/tsconfig.json"
}
]
}
์ฐธ๊ณ
์ค๋ฌด๋ก ํตํ๋ ํ์ ์คํฌ๋ฆฝํธ
์ค๋ฌด๋ก ํตํ๋ ํ์ ์คํฌ๋ฆฝํธ - ์์ค24
๋ฌธ์ ์ธ์๋ถํฐ ํด๊ฒฐ ๋ฐฉ์, ์ฌ๋ ์๋ ๋ ผ์๊น์ง.์ฝ๋์ ํ์ง์ ํ ๋จ๊ณ ์ ๊ทธ๋ ์ด๋ํ๋ ํ์ ์คํฌ๋ฆฝํธ ํ์ฉ๋ฒ์ค๋ฅ๋ฅผ ์ค์ด๊ณ ์์ฐ์ฑ์ ๋์ด๋ ์ต๊ณ ์ ๊ฐ์ด๋!ํ์ ์คํฌ๋ฆฝํธ์ ์ฃผ์ ๋ชฉ์ ์ ์๋ฐ์ค
m.yes24.com
'Front-end > TypeScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [WIP]TypeScript์ ๋์ ์๋ฆฌ (0) | 2025.10.25 |
|---|---|
| JavaScript๊ฐ ์๋ TypeScript๋ฅผ ์ฌ์ฉํ๋ ์ด์ (0) | 2025.10.25 |
| @types : JS ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ TS ํ๋ก์ ํธ์์ ์ฌ์ฉํ๊ธฐ (0) | 2025.07.03 |
| TypeScript์์์ ํ์ด์ง ์ด๋ : Link (0) | 2025.03.29 |
| ํ์ ์คํฌ๋ฆฝํธ(TypeScript)๋? (0) | 2025.03.26 |
GitHub