TypeScript สำหรับผู้เริ่มต้น - ทำไมคุณควรใช้
บทนำ
TypeScript เป็นภาษาที่สร้างบนพื้นฐาน JavaScript และเพิ่มการตรวจสอบประเภท
TypeScript คืออะไร?
TypeScript เป็น superset ของ JavaScript ที่เพิ่ม:
- Static type checking
- Interfaces
- Enums
- Generics
ทำไมควรใช้ TypeScript?
1. Type Safety
TypeScript ช่วยจับ errors ตั้งแต่เขียนโค้ด
2. Better IDE Support
TypeScript ให้ autocompletion และ inline documentation
3. Self-Documenting Code
Types ทำให้โค้ดเป็น self-documenting
Basic Types
const name: string = 'John';
const age: number = 30;
const isActive: boolean = true;
Interfaces
interface Person {
name: string;
age: number;
}
Functions
function add(a: number, b: number): number {
return a + b;
}
Classes
class Animal {
name: string;
constructor(name: string) {
this.name = name;
}
}
บทสรุป
TypeScript ช่วยให้เขียน JavaScript ที่ปลอดภัยและมีประสิทธิภาพ