面试题答案
一键面试// 声明基础接口Shape
interface Shape {
color: string;
}
// 声明Circle接口,继承自Shape
interface Circle extends Shape {
radius: number;
}
// 声明Rectangle接口,继承自Shape
interface Rectangle extends Shape {
width: number;
height: number;
}
// 声明draw函数
function draw(shape: Circle | Rectangle) {
if ('radius' in shape) {
console.log(`绘制圆形,颜色:${shape.color},半径:${shape.radius}`);
} else {
console.log(`绘制矩形,颜色:${shape.color},宽:${shape.width},高:${shape.height}`);
}
}