Skip to content
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

Destructing assignment fail to implicit cast type #26804

Closed
joshuaavalon opened this issue Aug 31, 2018 · 1 comment · Fixed by #44730
Closed

Destructing assignment fail to implicit cast type #26804

joshuaavalon opened this issue Aug 31, 2018 · 1 comment · Fixed by #44730
Labels
Duplicate An existing issue was already created

Comments

@joshuaavalon
Copy link

TypeScript Version: 3.1.0

Search Terms: destruct implicit type

Code

enum Kind {
    Square = "square",
    Rectangle = "rectangle"
  }
  
  type Shape =
    | {
        kind: Kind.Square;
        size: number;
      }
    | {
        kind: Kind.Rectangle;
        width: number;
        height: number;
      };
  
  function area(s: Shape) {
    switch (s.kind) {
      case Kind.Square: {
        let b = s.size * s.size;
        break;
      }
      default: {
        let b = s.width * s.height;
        break;
      }
    }
  }
  
  function area2(s: Shape) {
    const { kind } = s;
    switch (kind) {
      case Kind.Square: {
        // [ts]
        // Property 'width' does not exist on type 'Shape'.
        // Property 'width' does not exist on type '{ kind: Kind.Square; size: number; }'.
        let b = s.size * s.size;
        break;
      }
      default: {
        // [ts]
        // Property 'width' does not exist on type 'Shape'.
        // Property 'width' does not exist on type '{ kind: Kind.Square; size: number; }'.
        let b = s.width * s.height;
        break;
      }
    }
  }

Expected behavior:
No error

Actual behavior:

test.ts(34,19): error TS2339: Property 'size' does not exist on type 'Shape'.
  Property 'size' does not exist on type '{ kind: Kind.Rectangle; width: number; height: number; }'.
test.ts(34,28): error TS2339: Property 'size' does not exist on type 'Shape'.
  Property 'size' does not exist on type '{ kind: Kind.Rectangle; width: number; height: number; }'.
test.ts(38,19): error TS2339: Property 'width' does not exist on type 'Shape'.
  Property 'width' does not exist on type '{ kind: Kind.Square; size: number; }'.
test.ts(38,29): error TS2339: Property 'height' does not exist on type 'Shape'.
  Property 'height' does not exist on type '{ kind: Kind.Square; size: number; }'.
@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 31, 2018
@RyanCavanaugh
Copy link
Member

Duplicate of #12184

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants