π Search Terms
accessor
decorator
readonly
β
Viability Checklist
β Suggestion
Add readonly as a modifier for accessor fields.
π Motivating Example
Suppose you have an injection framework with @provides, @consumes, and @inject. Consider the example
class A {
@provides(Number)
readonly value: number;
@inject([Number])
accessor b = new B();
constructor(value: number) {
this.value = value;
}
}
class B {
@consumes(Number)
readonly value: number;
}
Note because @provides is a field decorator, it cannot know when value is assigned unless it's in the initializer. As a consequence, b will not have value injected since @provides doesn't know when to reinject. This is fixed if you do
class A {
@provides(Number)
readonly accessor value: number;
@inject([Number])
accessor b = new B();
constructor(value: number) {
this.value = value;
}
}
class B {
@consumes(Number)
readonly value: number;
}
π» Use Cases
- What do you want to use this for?
Accessor declarations that shouldn't be modified after being used in the constructor.
- What shortcomings exist with current approaches?
You can only mark a field with @readonly using JSDoc and just hope that other developers don't touch the field.
- What workarounds are you using in the meantime?
@readonly using JSDoc
π Search Terms
accessor
decorator
readonly
β Viability Checklist
β Suggestion
Add
readonlyas a modifier foraccessorfields.π Motivating Example
Suppose you have an injection framework with
@provides,@consumes, and@inject. Consider the exampleNote because
@providesis a field decorator, it cannot know whenvalueis assigned unless it's in the initializer. As a consequence,bwill not havevalueinjected since@providesdoesn'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
@readonlyusing JSDoc and just hope that other developers don't touch the field.@readonlyusing JSDoc