TypeScript: [Proposal] Limit the compiler error about overriding the base class property with getter/setter accessors pair to the `useDefineForClassFields : true` case only
As suggested by @sandersn, opening a sibling issue to track the proposal from the https://github.com/microsoft/TypeScript/pull/37894
Topic
Currently, when a property in the base class is overridden with a getter/setter pair, compiler always generates an error: Playground link
class BaseClass {
prop : string = 'base_class'
}
class SubClass extends BaseClass {
_prop : string = 'sub_class'
/*
ERROR: 'prop' is defined as a property in class 'BaseClass', but is overridden here in 'SubClass' as an accessor.
*/
get prop () : string {
return this._prop
}
set prop (value : string) {
this._prop = value
}
}
// try running this snippet with `useDefineForClassFields : true` and `useDefineForClassFields : false`
console.log((new SubClass).prop)
This error is motivated by the fact, that in modern JavaScript, class fields have DEFINE semantic and the above example won’t work as intuitively expected if useDefineForClassFields compiler option is set to true.
The property access augmentation pattern
Need to note, that historically nobody was ever using the DEFINE semantic for class properties. Pretty much all the JavaScript and TypeScript code in the world currently assumes the SET semantic for class fields.
SET semantic makes it possible to use the property access augmentation pattern. Its main use case is to trigger some arbitrary code on property read/write. This is very useful for variety of purposes and is done by simply overriding the property of the base class with getter/setter accessors pair.
For example, lets say we have some 3rd party library with a class with parent property.
class ExternalClass {
parent : this = undefined
children : this[] = []
appendChild (child : this) {
child.parent = this
}
}
In the whole library, the parent is assumed to be a regular property and is accessed with dot notation.
Now lets say we want to plug this 3rd party library in our codebase, which has reactive capabilities. We need to track the access to the parent property and trigger some extra code on property write:
const reactive : any = undefined
type Reactive<V> = V
class OurClass extends ExternalClass {
@reactive()
reactiveParent : Reactive<this> = undefined
private _parent : this
get parent () : this {
return this._parent
}
set parent (value : this) {
this._parent = value
this.reactiveParent = value
}
}
With this simple subclass, we can use the 3rd party library code unchanged and mirror the writes to the parent property to the reactiveParent (which is plugged into the rendering process elsewhere).
DEFINE / SET semantic
TypeScript and JavaScript historically were using SET semantic for class fields. Pretty much all the code in the world assumes it. However, TC39 committee decided that class fields should use DEFINE semantic and to provide the way to migrate to new behavior, TypeScript introduced the new compiler config, useDefineForClassFields, disabled by default, because its a breaking change. Obviously, the “classic”, historical behavior will need to be supported for a very long time.
With this option enabled, the “property access augmentation” pattern becomes invalid, as demonstrated by the example in the beginning (see however, the “Additional considerations” below). It is probably reasonable to generate a compiler error being discussed in this case.
With this option disabled, the pattern remains valid. In this case there are no reasons to limit the language expressiveness and restrict the user from using the pattern.
Proposal
Proposal is to generate the compiler error being discussed only for the “DEFINE” semantic (useDefineForClassFields : true) case.
Additional considerations
There’s a promise from TC39 committee to provide an opt-in escape route to the “classic” SET semantic for class fields, using a decorator. This means, that the “property access augmentation” pattern will probably remain valid in JavaScript, even for native class fields, a user will just need to opt-in for it, using the decorator.
Ideally, compiler will need to be smart, to determine which semantic the field is actually using, to avoid the needless restrictions. Perhaps the nature of this error is that it should be handled better by the linter, rather than compiler.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 12
- Comments: 24 (6 by maintainers)
If we’re being pedantic, this change singlehandedly prevents everyone from ever using public properties in any of their classes ever again, because it prevents others from ever extending their classes with new behaviour. When you’re designing a class, it’s impossible to know what someone else might wanna extend it with 10 years later. This change throws us back to the C++ hell of having useless public getters and setters for every attribute we’ll ever make, just in case someone else might need to override the behaviour in the unforeseeable future.
Worst change ever made in the history of the TypeScript repository. Other languages are fighting to implement property overriding, while TypeScript decided to proactively prevent it.
@sandersn
Its two people examples only in this thread. Plus 12 positive emojis. Plus many more examples in the previous thread, which itself has 12 negative emojis.
https://github.com/microsoft/TypeScript/pull/37894#issuecomment-678646084 +3 emojis https://github.com/microsoft/TypeScript/pull/37894#issuecomment-679190977 +12 emojis https://github.com/microsoft/TypeScript/pull/37894#issuecomment-679279784 +3 emojis https://github.com/microsoft/TypeScript/pull/37894#issuecomment-701947017 +13 emojis https://github.com/microsoft/TypeScript/pull/37894#issuecomment-717789311 +5 emojis https://github.com/microsoft/TypeScript/pull/37894#issuecomment-770318450 +1 emojis https://github.com/microsoft/TypeScript/pull/37894#issuecomment-800857078
This means in total, 2 + 12 + 12 +7 + 3 + 12 + 3 + 13 + 5 + 1 = 70 people are positive for this feature. This amount of developers are representative enough.
Can you share what codebases were examined in the survey? Are they written in the OOP approach? Do the authors of those codebases have the JavaScript background, or they come from other languages?
Because in our internal JavaScript code base, the property access augmentation pattern is used excessively. It is a valid “classic” JavaScript pattern, which assumes [[Set]] semantic for properties, its not clear, why TypeScript restricts it both for [[Define]] and [[Set]].