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

Object literal is not a valid instance of any of the types in the declared type union #40109

Closed
philipahlberg opened this issue Aug 18, 2020 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@philipahlberg
Copy link

TypeScript Version: 3.9.2 (or 3.5.1 and up)

Search Terms:
Union types, object literal

Code

interface AB {
  a: string;
  b: number;
}

interface C {
  c: boolean;
}

const abc: AB | C = {
  b: 123,
  c: true,
};

Expected behavior:
The declaration of the variable abc should not be valid.

The error could be something along the lines of:

The type '{ b: number; c: true }' is not assignable to type 'AB | C'.
	Object literal may only specify known properties, and 'b' does not exist in 'C', and 'c' does not exist in 'AB'.

Actual behavior:
The declaration of the variable abc is valid.

Playground Link: typescriptlang.org

Related Issues: Couldn't find any.

@MartinJohns
Copy link
Contributor

Duplicate of #20863 (and many many more). Excess property checks are not performed for union types.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 18, 2020
@RyanCavanaugh
Copy link
Member

Notably, object types are not sealed, so an excess property is not a per se error

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants