Skip to content

Possible bug: Hybrid between two interfaces of a union passes type checking #14383

Closed
@dsifford

Description

@dsifford

TypeScript Version: 2.1.1 / nightly (2.2.0-dev.20170228)

Went to StackOverflow with this first and the consensus there seems to be that this is a bug.

Here's my question again for convenience:


Consider a situation where an object can have exactly the FooBar or Baz interface listed below.

interface FooBar {
    foo: string;
    bar: string;
}

interface Baz {
    baz: string;
}

It's my understanding that this "one or the other" situation can be handled by creating a union between FooBar and Baz.

type FooBarOrBaz = FooBar | Baz;

So far so good...

The issue I'm having is that the following object passes type checking:

const x: FooBarOrBaz = {
    foo: 'foo',
    baz: 'foo',
};

Expected behavior: x should not pass type checking. It's neither FooBar nor Baz, but rather a combination between the two.

Actual behavior: x passes type checking.

Playground Example

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