Closed
Description
TypeScript Version: 3.1.0
Search Terms: destruct implicit type
Code
enum Kind {
Square = "square",
Rectangle = "rectangle"
}
type Shape =
| {
kind: Kind.Square;
size: number;
}
| {
kind: Kind.Rectangle;
width: number;
height: number;
};
function area(s: Shape) {
switch (s.kind) {
case Kind.Square: {
let b = s.size * s.size;
break;
}
default: {
let b = s.width * s.height;
break;
}
}
}
function area2(s: Shape) {
const { kind } = s;
switch (kind) {
case Kind.Square: {
// [ts]
// Property 'width' does not exist on type 'Shape'.
// Property 'width' does not exist on type '{ kind: Kind.Square; size: number; }'.
let b = s.size * s.size;
break;
}
default: {
// [ts]
// Property 'width' does not exist on type 'Shape'.
// Property 'width' does not exist on type '{ kind: Kind.Square; size: number; }'.
let b = s.width * s.height;
break;
}
}
}
Expected behavior:
No error
Actual behavior:
test.ts(34,19): error TS2339: Property 'size' does not exist on type 'Shape'.
Property 'size' does not exist on type '{ kind: Kind.Rectangle; width: number; height: number; }'.
test.ts(34,28): error TS2339: Property 'size' does not exist on type 'Shape'.
Property 'size' does not exist on type '{ kind: Kind.Rectangle; width: number; height: number; }'.
test.ts(38,19): error TS2339: Property 'width' does not exist on type 'Shape'.
Property 'width' does not exist on type '{ kind: Kind.Square; size: number; }'.
test.ts(38,29): error TS2339: Property 'height' does not exist on type 'Shape'.
Property 'height' does not exist on type '{ kind: Kind.Square; size: number; }'.