Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ole-flavia opened this issue Oct 23, 2019 · 1 comment

Comments

@ole-flavia
Copy link

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

@jack-williams
Copy link
Collaborator

Duplicate of #32639, and tracked at #12184.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants