Skip to content

TypeScript (.ts)

ไฟล์ .ts เป็นไฟล์หลักของ TypeScript ที่ประกอบด้วย:

  • โค้ด TypeScript ที่มี type annotations
  • สามารถ compile เป็น JavaScript (.js)
  • รองรับฟีเจอร์ทั้งหมดของ TypeScript

ตัวอย่าง

ts
// ประกาศฟังก์ชันพร้อม type
function greet(name: string): string {
  return `Hello, ${name}!`;
}

// ใช้งาน
console.log(greet('TypeScript'));

สิ่งที่ควรรู้

  • ไฟล์ .ts จะถูก compile เป็น .js ก่อนรัน
  • ต้องมี tsconfig.json สำหรับการตั้งค่า
  • รองรับ ES Modules และ CommonJS

/**

  • ตัวอย่างพื้นฐาน TypeScript
    1. การประกาศตัวแปรพร้อม type annotation
    1. การใช้งาน console.log */ const greeting: string = 'สวัสดี TypeScript'; console.log(greeting);

// ตัวอย่างเพิ่มเติม interface User { name: string; age: number; }

const user: User = { name: 'John', age: 30 };

console.log(User: ${user.name}, Age: ${user.age});