-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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 type not act as mutual exclusion #31055
Comments
I'd expect a EPC error (excess property checks) as well. But besides that your object literal fulfills the contract of type Playground link: https://typescript-play.js.org/#code/... Related Issues: #12936 and #12936 (comment) and #14094. |
The issue tracking this particular problem is #20863 --- there is no discriminant property for the union type. |
Union types are not intended to be automatically mutually exclusive |
@RyanCavanaugh |
For an assignment |
@RyanCavanaugh So can I understand this as:
But why |
The things that excess property checking warns about aren't things related to subtyping or assignability. |
@RyanCavanaugh is it intended for VSCode intellisense to provide auto completion for B if A is already declared in an object literal? |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
@RyanCavanaugh it doesn't make any sense, code above, because it doesn't fulfill A, neither does it fulfill B, it only fulfill A & B, union isn't exclusively but it should fulfill at least one of them, in this case it doesn't fulfill anyone of them. type A = { a: string };
type B = { b: string };
type T = A | B;
// error: Object literal may only specify known properties
const a: A = {
a: 'xxxxxxxxx',
b: 'yyyy'
};
// error: Object literal may only specify known properties
const b: B = {
a: 'xxxxxxxxx',
b: 'yyyy'
};
// what?
const t: T = {
a: 'xxxxxxxxx',
b: 'yyyy'
}; |
TypeScript Version: 3.4.3
Search Terms:
union mutual exclusion
Code
Expected behavior:
Compiled error.
ab
should be a mutual exclusion betweenA
andB
.A
orB
, shouldn't have 2 fields at the same time.B
is wrongActual behavior:
Compiled succ.
Playground Link:
Related Issues:
The text was updated successfully, but these errors were encountered: