Skip to content

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

Merged
merged 1 commit into from
Aug 25, 2021

Conversation

bwrrp
Copy link
Contributor

@bwrrp bwrrp commented Aug 1, 2021

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 error
  • tests/cases/compiler/mappedTypeRecursiveInference.ts - lib.dom.d.ts defines the type of document.defaultView as (WindowProxy & typeof globalThis) | null. It turned out that some of the properties (such as navigator) on WindowProxy 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?

@ghost
Copy link

ghost commented Aug 1, 2021

CLA assistant check
All CLA requirements met.

@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Aug 1, 2021
@bwrrp bwrrp force-pushed the intersection-readonly branch from 737d038 to be7275b Compare August 16, 2021 14:47
Copy link
Member

@sandersn sandersn left a 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.

@@ -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
Copy link
Member

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

@bwrrp bwrrp force-pushed the intersection-readonly branch from be7275b to c672831 Compare August 22, 2021 17:55
@bwrrp
Copy link
Contributor Author

bwrrp commented Aug 22, 2021

Thanks for the review! I've removed the unnecessary comments.

@sandersn
Copy link
Member

A last-minute 4.4 PR made a change to mappedTypeRecursiveInference. =( @bwrrp can you merge from main and update the baselines one more time?

@bwrrp bwrrp force-pushed the intersection-readonly branch from c672831 to 5a9ab1d Compare August 25, 2021 08:40
@sandersn sandersn merged commit 2a2962a into microsoft:main Aug 25, 2021
BobobUnicorn pushed a commit to BobobUnicorn/TypeScript that referenced this pull request Oct 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Intersection between separate types with getter and setter does not allow assignment to property
3 participants