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

Union types unexpected behavior #41509

Closed
rommeras opened this issue Nov 12, 2020 · 2 comments
Closed

Union types unexpected behavior #41509

rommeras opened this issue Nov 12, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@rommeras
Copy link

TypeScript Version: 4.0.5

Search Terms:
array of union types

Code

type A = {a: number; b?: number;}
type B = {c: number; d: number;}
type C = A | B;

const c: Array<C> = [
  {a: 5, c: 6},
];

Expected behavior:
Type '{ a: number; c: number; }' is not assignable to type 'C'. Property 'd' is missing in type '{ a: number; c: number; }' but required in type 'B'.

Actual behavior:
No errors are shown. Making b non optional property of type A gives expected error

Playground Link: https://www.typescriptlang.org/play?#code/C4TwDgpgBAglC8UDeBDAXFAdgVwLYCMIAnAbinwH4McDiSBfAKFEigCEFkBjavQ0qABNetUkxbQAwpzgAfdiUaMuAe0wBnYFB6wiRFCAA8kgHycA2oyjJ0UAKwAabRgBs9B4wC6JIA
Related Issues:

@MartinJohns
Copy link
Contributor

MartinJohns commented Nov 12, 2020

Duplicate of #20863. Your object literal is compatible with A, and excess property checks are not performed for union types (see mentioned issue).

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Nov 12, 2020
@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