-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Intersection between separate types with getter and setter does not allow assignment to property #45122
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 I agree. My intuition is that If that intuition holds for type U = Pick<{ readonly p: any } | { p: any }, "p">
// type U = { readonly p: any; } 🙂 I'd also expect intersections to not be type I = Pick<{ readonly p: any } & { p: any }, "p">
// type I = { readonly p: any; } 🙁 Oops. |
This (the OP example) only even became legal in 4.3; it's an oversight that this particular combination wasn't made to work. The definition of intersection implies that this should be legal. |
While checking this change against the existing unit tests, I came across a change in behavior in the DOM typings I'm not sure about. The I'm not sure what the idea is behind using an intersection type here instead of defining any missing globals as Window properties? Given that |
The defaultView definition comes from microsoft/TypeScript-DOM-lib-generator#820. It's basically copying microsoft/TypeScript-DOM-lib-generator#715, which did the same thing for Like most intersections in the real world, properties appearing in both sides are unfortunate and hopefully identical. In this case, I think it's fine to lose the readonly-ness of the |
Bug Report
I'm trying to define a property that is writable in some cases and readonly in others, by defining its getter and setter in different types and using the intersection between those when the property should be writable. However, TypeScript still complains about the property not being writable.
🔎 Search Terms
intersection getter setter readonly
🕗 Version & Regression Information
Reproducible on the playground with both the latest stable (4.3.5) as well as with 4.4.0 beta and the 'nightly' option.
⏯ Playground Link
Playground link with relevant code
Alternative attempt using
readonly
💻 Code
I've tried the following approaches - using a getter and setter:
and also using the
readonly
keyword:🙁 Actual behavior
In both cases the assignment is not allowed by the compiler, and I got a
Cannot assign to 'prop' because it is a read-only property.
error.🙂 Expected behavior
I expected
A & B
to behave as "This type implements both A and B at the same time". Because of that I would have expected a readonly property defined in A to be writable if B provides a setter.The text was updated successfully, but these errors were encountered: