-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Assignment of a partial type member to an intersection type including the partial does not compile in 2.7.0-dev.20180112 #21170
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
Comments
I think this is appropriate. If |
It is worth nothing that this is a result of #17912. As @DanielRosenwasser noted, the current behavior is correct. since |
Here's what I think would be a better workaround: interface X {
x: boolean;
}
function fn<T extends Partial<X>>(obj: T, props: Partial<T>): (T & X) {
const xobj = obj;
xobj.x = true;
for (const p in props) {
xobj[p] = props[p];
}
return xobj as T & X;
} Here, you ensure that common properties of |
Ah, I overlooked the case of overlapping keys in the type intersection. Thanks for the explanation and the workaround, @DanielRosenwasser and @mhegazy. |
@DanielRosenwasser The workaround suggested fails with
To get past this problem, I cast interface X {
x: boolean;
}
function fn<T extends Partial<X>>(obj: T, props: Partial<T>): (T & X) {
const xobj = obj;
xobj.x = true;
for (const p in props) {
xobj[p] = props[p] as T[keyof T]; // <---
}
return xobj as T & X;
} |
TypeScript Version: 2.7.0-dev.20180112
Code
Expected behavior:
This should compile since the members of
Partial<T>
is a subset of the members of(T & IX)
.This does compile in 2.7.0-dev.20180108.
Actual behavior:
The following error is emitted when compiled:
Related:
The text was updated successfully, but these errors were encountered: