microsoft/TypeScript

No error on object literal containing property of wrong type for union

pelotom opened this issue · 5 comments

TypeScript Version: 2.6.2, 2.7.2, 2.8.0-dev.20180222

Search Terms:

Code

export interface A {
  x?: string;
}
export interface B extends A {
  y?: number;
}
export interface C extends A {
  z?: boolean;
}

const passes: B | C = { x: 'hello', z: 42 };

Expected behavior:

I would expect to get this type of error:

Types of property 'z' are incompatible.
  Type 'number' is not assignable to type 'boolean | undefined'.

Actual behavior:

No error. I would guess that the object literal is being inferred as a B with an extraneous z property instead of a C, however I would expect the rule about object literals not containing extraneous properties to apply. And what's odder is that if x isn't specified it does give the expected error:

const fails: B | C = { z: 42 };

Playground Link

Looks like a duplicate of #20863

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

According to #20863 (comment), this is not a duplicate. Could it be reopened?

Probably the error is that the object provided is assignable to B -- this should be considered an excess property and we should compare with C instead, which gives a better error.

I believe the comment in #20863 (comment) is not right. this is a duplicate of #20863.