Union Types
การกำหนดประเภทข้อมูลที่เป็นได้หลายชนิด
typescript
let id: number | string;
id = 10; // Valid
id = "123"; // Valid
Intersection Types
การรวมหลายประเภทข้อมูลเข้าด้วยกัน
typescript
interface ErrorHandling {
success: boolean;
error?: { message: string };
}
interface ArtworksData {
artworks: { title: string }[];
}
type ArtworksResponse = ArtworksData & ErrorHandling;
let response: ArtworksResponse = {
success: true,
artworks: [{ title: "Mona Lisa" }]
};