Description
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.
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about getter / setter / readonly / intersection types
⏯ Playground Link
Playground link with relevant code
Alternative attempt using readonly
💻 Code
I've tried the following approaches - using a getter and setter:
type T = {get prop(): string;} & {set prop(v: string)};
function aaa(a: T) {
a.prop = 'aaa';
}
and also using the readonly
keyword:
type T = {readonly prop: string;} & {prop: string};
function aaa(a: T) {
a.prop = 'aaa';
}
🙁 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.