- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.1k
Closed
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
TypeScript Version: 3.1.1
Search Terms:
- type inference, destructuring, type narrowing
Code
interface A {
  hasValue: true;
  value: string;
}
interface B {
  hasValue: false;
}
function checkOne(args: A | B) {
  return args.hasValue && args.value;
  //                           ^^^^^ this is ok, 'args' infers type A
}
function checkTwo(args: A | B) {
  const { hasValue } = args;
  return hasValue && args.value;
  //                      ^^^^^ Error: Property 'value' does not exist on type 'B'.
}Expected behavior:
Both checkOne and checkTwo compiled without errors.
Actual behavior:
Function checkTwo has compilation errors.
Playground Link: typescriptlang.org
Related Issues: I can't find related issues, but I am sure this is already known issue.
LennyLip, cogell, Sokushinbutsu, bhavin-a, mkermani144 and 1 more
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created