Closed
Description
TypeScript Version: 3.8.0-dev.20191022
Search Terms: destructuring
Code
type X = {x: true, y: number[]};
type Y = {x: false, y: undefined};
type Z = X | Y;
function a(x: Z) {
if (x.x) {
x.y.forEach(console.log);
}
}
function b({x, y}: Z) {
if (x) {
y.forEach(console.log);
// ^ Object is possibly 'undefined'
}
}
Expected behavior:
Function b
should compile.
Actual behavior:
Typescript does not detect any relation between x
and y
variables in function b
, so it does not figure out that x === true => y is number[]
.
Related Issues: None
Metadata
Metadata
Assignees
Labels
No labels