Skip to content

Type inference behave different with or without destructure #28599

Closed
@lihz6

Description

@lihz6

TypeScript Version: 3.2.0-dev.201xxxxx

Search Terms: type inference behave different

Code

type Shape = {
  kind: 'circle',
  data: {
    radius: number
  }
} | {
  kind: 'square',
  data: {
    size: number
  }
};

function getAreaOkay(shape: Shape) {
  switch (shape.kind) {
    case 'circle':
      // okay
      return shape.data.radius;
  }
}
function getAreaFail({ kind, data }: Shape) {
  switch (kind) {
    case 'circle':
      // fail
      return data.radius;
  }
}

Expected behavior: the above two should be identical.

Actual behavior: they are not.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions