Skip to content

Discriminated union types better error reporting #10867

@wclr

Description

@wclr

This the example from docs:
image

interface Square {
    kind: "square";
    size: number;
}

interface Rectangle {
    kind: "rectangle";
    width: number;
    height: number;
}

interface Circle {
    kind: "circle";
    radius: number;
}

type Shape = Square | Rectangle | Circle;

let shape: Shape = {
  kind: "square",
  width: 10,
  height: 10
}

So we create object with type Shape of kind square and try to pass incorrect other props (for this kind), and TS emits the error that object is no assignable to last type that is in union (Circle in this case). I believe it would be better if TS would recoginze that we are trying to make Square type in this case and emitted corresponding error message.

Metadata

Metadata

Assignees

Labels

CommittedThe team has roadmapped this issueFixedA PR has been merged for this issueSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions