镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Destructing assignment fail to implicit cast type #26804

Description

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; }'.

Activity

  1. RyanCavanaugh commented on Aug 31, 2018

    @RyanCavanaugh
    Member

    Duplicate of #12184

  2. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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