Closed
Description
TypeScript Version: 3.2.0-dev.201xxxxx
Search Terms: type inference behave different
Code
type Shape = {
kind: 'circle',
data: {
radius: number
}
} | {
kind: 'square',
data: {
size: number
}
};
function getAreaOkay(shape: Shape) {
switch (shape.kind) {
case 'circle':
// okay
return shape.data.radius;
}
}
function getAreaFail({ kind, data }: Shape) {
switch (kind) {
case 'circle':
// fail
return data.radius;
}
}
Expected behavior: the above two should be identical.
Actual behavior: they are not.