-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Allow readonly
with accessor
#55289
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
This seems intentional: #51820 And in the implementing PR it's explicitly mentioned: #49705
See also this comment: #49705 (comment) |
I see. @rbuckton FWIU from #49705 (comment), the blocker seems to be that accessors also create a setter? I think this is expected. In fact, it's necessary that the accessor creates the setter even in a readonly situation. The reason is |
@RyanCavanaugh @rbuckton Any updates here? |
I believe this would directly conflict with the Grouped and Auto Accessors Proposal, and I would rather not have two syntaxes for this. For the time being, are you able to use a normal getter? If so, this might be an option: class A {
#value: number;
@provides(Number)
get value() { return this.#value; }
@inject([Number])
accessor b = new B();
constructor(value: number) {
this.value = value;
}
} If not, and you are using native decorators, then you could work around this through indirection: class A {
@provides(Number)
#value: number;
get value() { return this.#value; }
@inject([Number])
accessor b = new B();
constructor(value: number) {
this.#value = value;
}
} If you are using our legacy class A {
@provides(Number)
private _value: number;
get value() { return this._value; }
@inject([Number])
accessor b = new B();
constructor(value: number) {
this._value = value;
}
} I should also note that in the Grouped and Auto-Accessors proposal, this would be accomplished as follows (though the syntax is subject to change at this early stage): class A {
@provides(Number)
accessor value: number { get; #set; }
@inject([Number])
accessor b = new B();
constructor(value: number) {
this.#value = value;
}
} |
I'm afraid the proposed solutions do not work. We are using native decorators and require the field to be an accessor. Using a normal getter is possible, but for every instance is a lot of boilerplate. I guess I'll put an issue up about this in ts-eslint. [EDIT: NVM, I just learned that |
π Search Terms
accessor
decorator
readonly
β Viability Checklist
β Suggestion
Add
readonly
as a modifier foraccessor
fields.π Motivating Example
Suppose you have an injection framework with
@provides
,@consumes
, and@inject
. Consider the exampleNote because
@provides
is a field decorator, it cannot know whenvalue
is assigned unless it's in the initializer. As a consequence,b
will not havevalue
injected since@provides
doesn't know when to reinject. This is fixed if you doπ» Use Cases
Accessor declarations that shouldn't be modified after being used in the constructor.
You can only mark a field with
@readonly
using JSDoc and just hope that other developers don't touch the field.@readonly
using JSDocThe text was updated successfully, but these errors were encountered: