I'm trying to understand how typescript works here. For object with similar object properties, I'm able to handle. But how about something like this?
Shape has different object values. How can I run this function without an error?
type Shape =
| { kind: "circle"; radius: number }
| { data: "square"; x: number };
function area(s: Shape) {
if (s.kind === "circle") {
return Math.PI * s.radius * s.radius;
} else {
return s.x * s.x;
}
}
PS: This is a broken code.