type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; sideLength: number };
function area(shape: Shape): number {
const isCircle = shape.kind === "circle";
if (isCircle) {
// We know we have a circle here!
return Math.PI * shape.radius ** 2;
} else {
// We know we're left with a square here!
return shape.sideLength ** 2;
}
}
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; sideLength: number };
// 위의 코드는 아래와 같다
type Shape = {
kind: "circle";
radius: number;
} | {
kind: "square";
sideLength: number;
}