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

Union of two interfaces rejects keys that should be acceptable #18421

Closed
dylanpyle opened this issue Sep 12, 2017 · 5 comments · Fixed by #30779
Closed

Union of two interfaces rejects keys that should be acceptable #18421

dylanpyle opened this issue Sep 12, 2017 · 5 comments · Fixed by #30779
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@dylanpyle
Copy link

dylanpyle commented Sep 12, 2017

Update: I submitted a bad code sample the first time around — I've updated this to reflect something that seems actually broken.

--

TypeScript Version: 2.5.2

Code

interface ThingTypeOne {
  type: 'one';
}

interface ThingTypeTwo {
  type: 'two';
}

type ThingType = 'one' | 'two';

type Thing = ThingTypeOne | ThingTypeTwo;

function makeNewThing(thingType: ThingType): Thing {
  return {
    type: thingType
  };
}

Expected behavior:

No error

Actual behavior:

foo.ts(14,3): error TS2322: Type '{ type: ThingType; }' is not assignable to type 'Thing'.
  Type '{ type: ThingType; }' is not assignable to type 'ThingTypeTwo'.
    Types of property 'type' are incompatible.
      Type 'ThingType' is not assignable to type '"two"'.
        Type '"one"' is not assignable to type '"two"'.

It looks to me like the issue is that the compiler doesn't figure out the union of keys within the interfaces. If that's true, it seems strange to me that this is a breaking error since this code should work just fine.

@ahejlsberg
Copy link
Member

type Thing = ThingTypeTwo | ThingTypeTwo;

I'm pretty sure you meant to say:

type Thing = ThingTypeOne | ThingTypeTwo;

@ahejlsberg ahejlsberg added the Working as Intended The behavior described is the intended behavior; this is not a bug label Sep 12, 2017
@dylanpyle dylanpyle changed the title Partial of union type disallows one side of the union Union of two interfaces rejects keys that should be acceptable Sep 12, 2017
@dylanpyle
Copy link
Author

@ahejlsberg Yep sorry about that, I've updated the original description with a new failing code sample that should illustrate this better, and updated the issue title to match.

@dylanpyle dylanpyle reopened this Sep 12, 2017
@olegdunkan
Copy link

olegdunkan commented Sep 13, 2017

@dylanpyle { type:"one" } | { type:"two" } is not the same as { type:"one" | "two" }

@mhegazy
Copy link
Contributor

mhegazy commented Oct 3, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Oct 3, 2017
@jcalz
Copy link
Contributor

jcalz commented Oct 30, 2017

@olegdunkan et al,

Could someone explain why { type:"one" } | { type:"two" } is not the same as { type:"one" | "two" }? I understand that the compiler is not recognizing the identity, but they seem identical to me. Can you produce an object of one type that is not an object of the other? I am unable to conceive of one.

EDIT: Ah, this issue is a duplicate of #12052, in which it is mentioned by @ahejlsberg that these two types are equivalent, but the compiler doesn't establish the equivalence because it is rare in practice to encounter a union type that can be represented as a non-union type with a union property. Now I have a good answer to an SO question.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants