-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Can't partial a generic type with extension constraint #13442
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
The error is correct. We know that |
I'm not wholly sure that I understand. Presumably for the specific type that Document eventually is, that mandates the selection of SelectionVisibility to be that property's type. So the visibilityUpdates parameter must be of that same type (or extending it), which should be legal. Could you show an example of the error case you're describing? |
A simpler example is helpful, since interface Point2d { x: number, y: number };
interface Point3d { x: number, y: number, z: number };
function foo<T, U extends { prop: T }>(a: T, b: U): U {
return { prop: a }; // Disallowed, see next comment for why
}
var p2: Point2d, p3: Point3d;
// m: { prop: Point3d } m.prop.z does not actually exist
let m = foo(p2, { prop: p3 }); |
Yeah, OK, that is nasty. |
Wait, doesn't this present a massive hole in the structural typing in general, regardless of any generics?
Or to put it another way, your example can be trivially modified to compile and still break the type system
Seems to me like unless the property is readonly, you must assume that the function could write to it, and so can't permit covariance here. |
Absolutely; TypeScript is not 100% sound in that regard. |
I don't suppose there are any plans to change this in the future? I'm suddenly becoming aware that there would be a lot of breakage going on if this were fixed. I guess that although my original example is admittedly unsafe, I'm not sure that it's any more unsafe than the permitted examples. It seems odd to refuse to compile in this case to favour correctness over convenience when the correctness horse left the stable a long, long time ago. If it's not going to be fixed and we favour convenience, then I'd say that the lack of convenience in this case is a bug. |
TypeScript Version: 2.1.5
Expected behavior:
Compiles.
Actual behavior:
Doesn't compile. The type { sectionVisibility: SectionVisibility } is not assignable to Partial, despite the fact that we clearly constrained Document to include this property, and it's Partial so we know that every other property is optional.
The text was updated successfully, but these errors were encountered: