面试题答案
一键面试type MyUnion = { type: 'circle'; radius: number } | { type:'rectangle'; width: number; height: number };
function calculateArea(shape: MyUnion): number {
if (shape.type === 'circle') {
return Math.PI * shape.radius * shape.radius;
} else {
return shape.width * shape.height;
}
}