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

Discriminated union types better error reporting #10867

Closed
wclr opened this issue Sep 12, 2016 · 4 comments
Closed

Discriminated union types better error reporting #10867

wclr opened this issue Sep 12, 2016 · 4 comments
Assignees
Labels
Committed The team has roadmapped this issue Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@wclr
Copy link

wclr commented Sep 12, 2016

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.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Sep 12, 2016
@Arnavion
Copy link
Contributor

There's another recent issue #10849 that talks about this and other discriminated unions issues. Perhaps one could be merged into the other?

@KavanShaban
Copy link

Yes, this is a subset of the issue #10849 that reported a few days back.

@RyanCavanaugh RyanCavanaugh added Committed The team has roadmapped this issue and removed In Discussion Not yet reached consensus labels Nov 15, 2016
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 2.2 milestone Nov 15, 2016
@RyanCavanaugh
Copy link
Member

Work item for 2.2: Figure out how to detect discriminated unions and report errors based on a matched discriminant field

@mhegazy mhegazy modified the milestones: TypeScript 2.2, TypeScript 2.3 Feb 2, 2017
sandersn added a commit that referenced this issue Feb 10, 2017
Assignability errors for discriminated unions now check the value of the
discriminant to decide which member of the union to check for
assignability.

Previously, assignability didn't know about discriminated unions and
would check every member, issuing errors for the last member of the
union if assignability failed.

For example:

```ts
type Square = { kind: "sq", size: number }
type Rectangle = { kind: "rt", x: number, y: number }
type Circle = { kind: "cr", radius: number }
type Shape =
    | Square
    | Rectangle
    | Circle;
let shape: Shape = {
    kind: "sq",
    x: 12,
    y: 13,
}
```

`typeRelatedToSomeType` now checks whether each property in the source
type is a discriminant. It finds `kind` and proceeds to look for the
type in the target union that has `kind: "sq"`. If it finds it, which it
does in this example (`Square`), then it checks only assignbility to
`Square`.

The result is that the error now says that property 'size' is missing in
type `{ kind: "sq", x: number, y: number }` instead of saying that that
"sq" is not assignable to type "cr" like it did before.

Fixes #10867
@sandersn
Copy link
Member

Fix is up at #14006

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Feb 13, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Committed The team has roadmapped this issue Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants