Model a Shape as a discriminated union and write area(shape) that returns its area, narrowing on the kind field.
type Shape =
| { kind: "circle"; radius: number }
| { kind: "rectangle"; width: number; height: number }
| { kind: "square"; size: number }Signature: area(shape: Shape) => number
π r²width * heightsize²Use a switch (shape.kind) so TypeScript narrows each branch. Add a default that assigns to never for exhaustiveness.
Switch on shape.kind; inside each case the other fields are available.
Circle area is Math.PI * shape.radius ** 2.
In default, const _exhaustive: never = shape catches missed cases.
Test natijalarini ko'rish uchun kodingizni ishga tushiring.