Skip to content

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

Closed
@dylanpyle

Description

@dylanpyle

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions