-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Type is cannot satisfy A and cannot satisfy B but satisfies A | B #45339
Comments
Excess property checking ( What we found in real-world code is that there were many cases where people wrote unions that didn't capture the full extent of allowed properties, so excess property checking allows excess properties as long as a matching property name is found anywhere in the target union. |
@RyanCavanaugh this seems weird. I mean That is how I would expect It just seems illogical that by design X != A, X != C, X == A | C So if this is by design, then is there a way to actually achieve this, so that a type MUST satisfy one of the union types NOT a mishmash of parts of different types in the union? |
const c: A | C = { // OK!
foo: 3,
other: 'cool',
} The initialization here is a valid const d = {
foo: 3,
other: 'cool',
};
const c: A | C = d; |
@RyanCavanaugh but that is bypassing the object literal. I could also do: const d = {
foo: 3,
other: 'cool',
};
const a: A = d; even though I cannot do: const a: A = {
foo: 3,
other: 'cool', //'other' does not exist in type
} I was under the impression that extra properties were OK when using a variable but NOT OK when using an object literal. So it seems like you can bypass the object literal restriction on excess properties as long as the type is a union? |
See #20863. |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Version & Regression Information
v4.3.5
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
c
is OK.π Expected behavior
If the same structure cannot satisfy A and cannot satisfy B it should be logical to assumed that it cannot satisfy A | B but here it does.
I would expect that since I have included
other
it cannot beA
and since I have not included$inherit
it cannot beC
so I would expect this to fail. I would expect that I could only makeconst c
ok by either removingother: cool
or by adding$inherit: "b"
The text was updated successfully, but these errors were encountered: