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