Dark mode
Collection Types (ประเภทชุดข้อมูล)
Array
ts
let numbers: number[] = [1, 2, 3];
let names: Array<string> = ["John", "Jane"]; // Generic syntax
// ใช้งานกับฟังก์ชัน
function sum(numbers: number[]): number {
return numbers.reduce((a, b) => a + b, 0);
}
Tuple
อาร์เรย์ที่กำหนดประเภทและลำดับของสมาชิก
ts
// Tuple type ใน TypeScript
let person: [string, number] = ["John", 30];
// ตัวอย่างการใช้งาน Tuple
function processPerson(person: [string, number]) {
const [name, age] = person;
console.log(`Name: ${name}, Age: ${age}`);
}
processPerson(person);
Enum
ts
enum Direction {
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT",
}
let move: Direction = Direction.Up;