Dark mode
ตั้งค่าโปรเจค TypeScript
1: สร้างไฟล์ package.json
- สร้างไฟล์ package.json ด้วยคำสั่ง:
bash
bun init -y
- ติดตั้ง dependencies ที่จำเป็น:
bash
bun add typescript
bun add -d @types/node
2: ตั้งค่า tsconfig.json
- สร้างไฟล์ configuration:
bash
npx tsc --init
- ตั้งค่าพื้นฐานในไฟล์ tsconfig.json:
json
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"moduleResolution": "bundler",
"outDir": "./dist"
},
"include": ["src/**/*"]
}
3: สร้างโครงสร้างโปรเจค
text
project/
├── src/
│ └── index.ts
├── dist/ # compiled output
├── tsconfig.json
└── package.json
4: เขียนโค้ดตัวอย่าง
สร้างไฟล์ src/index.ts
:
ts
// ตัวอย่างฟังก์ชัน TypeScript
function greet(name: string): string {
return `Hello, ${name}!`;
}
console.log(greet("TypeScript"));