-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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 of types contains only the keys that exist in both subtypes #35224
Comments
Here's a relevant StackOverflow answer regarding union types. With a union type you only get access to what is known to be present in both types.
But that would be wrong. For example if you have this interface:
This type is compatible to |
interface Supertype {
commonField: string;
uniqueField?: string;
} This is only valid when there is a one property delta, but it doesn't work generally. interface Subtype1 {
commonField: string;
}
interface Subtype2 {
commonField: string;
uniqueField1: string;
uniqueField2: string;
}
interface Supertype {
commonField: string;
uniqueField1?: string;
uniqueField2?: string;
}
|
Thank you for your answers! That clarifies the problem for me, I will close the issue. |
I see some inconsistency in the type system regarding union types. In my opinion the current implementation is quite confusing. Please see the example below.
TypeScript 3.7.2
Playground link
Input:
Output:
This line fails:
Expected behavior:
This line compiles fine:
In my understanding,
Supertype
type defined above should evaluate to something like:Whereas, from the playground example, we can see that it evaluates to:
I would not expect that. Can you please explain the reasoning behind such behavior? Sorry for the trouble, I imagine that is a design choice, but I did not find any source on why it is implemented like so.
The text was updated successfully, but these errors were encountered: