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

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

Closed
pelotom opened this issue Feb 22, 2018 · 5 comments
Closed

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

pelotom opened this issue Feb 22, 2018 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@pelotom
Copy link

pelotom commented Feb 22, 2018

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

@pelotom pelotom changed the title No error No error on property of wrong type Feb 22, 2018
@pelotom pelotom changed the title No error on property of wrong type No error on object literal containing property of wrong type for union Feb 22, 2018
@ghost
Copy link

ghost commented Feb 22, 2018

Looks like a duplicate of #20863

@typescript-bot
Copy link
Collaborator

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

@pelotom
Copy link
Author

pelotom commented Mar 19, 2018

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

@ghost ghost reopened this Mar 19, 2018
@ghost ghost added Bug A bug in TypeScript and removed Duplicate An existing issue was already created labels Mar 19, 2018
@ghost
Copy link

ghost commented Mar 19, 2018

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.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 12, 2018

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

@mhegazy mhegazy added Duplicate An existing issue was already created and removed Bug A bug in TypeScript labels Apr 12, 2018
@pelotom pelotom closed this as completed Apr 26, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants