Recently, while delving into the valuable resource titled Effective TypeScript by Dan Vanderkam, I stumbled across an intriguing scenario that left me puzzled. Within a code snippet presented in the book, there was a line - shape;
that seemed perplexing to me. Despite appearing straightforward at first glance, it failed to resonate with my understanding. What is the significance of this shape;
statement? Moreover, why is it repeated in the code? Could this possibly be a typo?
interface Square {
kind: 'square';
width: number;
}
interface Rectangle {
kind: 'rectangle';
height: number;
width: number;
}
type Shape = Square | Rectangle;
function calculateArea(shape: Shape) {
if (shape.kind === 'rectangle') {
shape; // I find this confusing!
return shape.width * shape.height;
} else {
shape; // Puzzling, isn't it?
return shape.width * shape.width;
}
}