Run ❯
Get your
own Node
server
❯
Run Code
Ctrl+Alt+R
Change Orientation
Ctrl+Alt+O
Change Theme
Ctrl+Alt+D
Go to Spaces
Ctrl+Alt+P
// In TypeScript, these would be properly typed interfaces // and a union type function calculateArea(shape) { switch (shape.kind) { case "circle": // TypeScript knows shape is Circle here return Math.PI * shape.radius ** 2; case "square": // TypeScript knows shape is Square here return shape.sideLength ** 2; } } // Example usage const circle = { kind: "circle", radius: 5 }; const square = { kind: "square", sideLength: 4 }; console.log(`Circle area: ${calculateArea(circle).toFixed(2)}`); console.log(`Square area: ${calculateArea(square)}`);
Circle area: 78.54 Square area: 16