type Artwork = {
  genre: string;
  name: string;
};

type Writing = {
  pages: number;
  name: string;
};

type WrittenArt = Artwork & Writing;

위는 아래와 같음.

{
  genre: string;
  name: string;
  pages: number;
}

type ShortPoem = { author: string } & (
  | { kigo: string; type: "haiku" }
  | { meter: number; type: "villanelle" }
);

// ✅ Ok
const morningGlory: ShortPoem = {
  author: "Fukuda Chiyo-ni",
  kigo: "Morning Glory",
  type: "haiku",
};

// ❌ error
const oneArt: ShortPoem = {
  author: "Elizabeth Bishop",
  type: "villanelle",
};

// ❌ error
const twoArt: ShortPoem = {
  author: "Elizabeth Bishop",
  kigo: "dd",
  type: "villanelle",
};

// ✅ Ok
const threeArt: ShortPoem = {
  author: "Elizabeth Bishop",
  meter: 20,
  type: "villanelle",
};

📍 교차 타입의 위험성

☑️ 긴 할당 가능성 오류

// bad
type ShortPoem = { author: string } & (
  | { kigo: string; type: "haiku" }
  | { meter: number; type: "villanelle" }
);

// good
type ShortPoemBase = { author: string };
type Haiku = ShortPoemBase & { kigo: string; type: "haiku" };
type Villanelle = ShortPoemBase & { meter: number; type: "villanelle" };
type ShortPoem = Haiku | Villanelle;

☑️ never

type NotPossible = number & string; // 타입: never