Skip to content

Destructuring for declaration prevents entangled type information / type implications #34667

Closed
@ole-flavia

Description

@ole-flavia

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[].

Playground Link: http://www.typescriptlang.org/play/?ts=3.8.0-dev.20191022&ssl=1&ssc=1&pln=17&pc=1#code/C4TwDgpgBAGlC8UDeAPAXFYAnArhANFCBgHY4C2ARhFgNoC6AvgNwBQokUAmgsulADMAhgBsAzgSIYcJACYQBASxIRZLduGgAtXnAA+3NqwEyAxsEUB7ElCEAKfloCUyVlCiKBUBwDoULpDd3KBQfEB8BSywAUSFTAAs7U2sxSxEIHxFLAHMnNndGVkLjMwtrKEo7VEIQRgxnV3dPb39G4PDImLjE5JJU9MycvLcAehGoAD0oAHlKACsIcw8xKDBLMTFFShEQKAByGXklFVk9oMLCoA

Related Issues: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions