-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Properties on intersections should be readonly only if all declarations are #45263
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
Conversation
737d038
to
be7275b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me, although I'd recommend removing the comments in checker.ts. But I'll merge as-is if you let me know that you want them to stay.
src/compiler/checker.ts
Outdated
@@ -11825,8 +11825,15 @@ namespace ts { | |||
} | |||
} | |||
} | |||
checkFlags |= (isReadonlySymbol(prop) ? CheckFlags.Readonly : 0) | | |||
(!(modifiers & ModifierFlags.NonPublicAccessibilityModifier) ? CheckFlags.ContainsPublic : 0) | | |||
// For unions, prop is readonly if any definition of it is readonly |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these comments seem superfluous to me -- the intersection one might need a comment if it were on its own, but here it's clearly the inverse of the union case, and the union case is clear as-is
be7275b
to
c672831
Compare
Thanks for the review! I've removed the unnecessary comments. |
A last-minute 4.4 PR made a change to mappedTypeRecursiveInference. =( @bwrrp can you merge from main and update the baselines one more time? |
c672831
to
5a9ab1d
Compare
Fixes #45122
This changes the behavior of readonly properties in intersection types to be writable if any of the definitions of the property are writable. Before, the property would only be writable if none of its definitions were readonly (same as for unions).
I've added a new test containing the two cases from the issue, as well as a case that verifies that the property is still readonly if all of the definitions are readonly. I also had to update the baseline for two existing tests:
tests/cases/conformance/types/intersection/intersectionTypeReadonly.ts
- contained a test for the mutable & readonly case, which I've updated to not expect an errortests/cases/compiler/mappedTypeRecursiveInference.ts
-lib.dom.d.ts
defines the type ofdocument.defaultView
as(WindowProxy & typeof globalThis) | null
. It turned out that some of the properties (such asnavigator
) onWindowProxy
are readonly, while the corresponding global declaration is not. This change makes those properties writable in the intersection type.It seems a little odd that properties defined as readonly on
Window
are considered writable on the global. I'm not sure if that is something that needs to be addressed here?