-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Allow this
parameter in property accessors (getter/setter)
#52923
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
FWIW I don't think having |
Related Question/bug report: #39254 |
I'm currently working on a block editor, so don't ask me why blocks have so many distributed classes. You know that many distribution classes support method(this:Block), but get parameter(this:Block) will get an error.
property accessors can be thought of as a kind of computation property, which is essentially the same as a method, so why do you support methods, but not property accessors property accessors (getters/setters) and methods are in the same domain and are usually not separated. I have to break them up now because the grammar doesn't support them. Can you explain the reason why you don't support them? Because supporting this creates other problems. I think ChatGPT would approve Allow this parameter in property accessors (getter/ setters) |
what. |
All features start at minus 100
Please stop π₯² |
Wait, that's actually a thing now? I was so confused by this, it's just a total non-sequitur, I thought there was a joke I wasn't getting. |
I have a very similar use case. The getters and setters are being passed in a "prototype" and Object.defineProperties are used to attach them to a target object later when the object is instantiated. In this case, the Also, it works with standard functions (where I can specify Despite the actual to-ing & fro-ing in the related tickets about whether this is possible, "valid" or desirable, this is perfectly valid JS use case, and mixins etc use it quite commonly, and being able to accurately model them in Typescript would be as advantageous as modelling any of the other JS paradigms. Anyone in TS dev able to point me in the right direction if I want to try implementing this? |
I think this is WRONG fixed for #36883 from #36883 interface Unimplemented {
calculate(): number;
}
class Demo {
get a(this: Unimplemented) {
return this.calculate();
}
b(this: Unimplemented) {
return this.calculate();
}
}
const x = new Demo();
console.log(x.a); // no type error, fails at runtime since this code is cheating/instrucmenting TS compiler the |
Suggestion
Allow the
this
parameter in property accessors:π Search Terms
getter setter accessor this argument parameter
β Viability Checklist
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript/JavaScript code
As with functions, the
this
parameter for accessors is optional and would default to the home object.This wouldn't change the runtime behavior of existing JavaScript code
As with most of TypeScript, this proposal has no effect on emitted JavaScript.
This could be implemented without emitting different JS based on the types of the expressions
As demonstrated below, this proposal technically already emits functioning JavaScript. This proposal only affects type checking (as with functions).
This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
This proposal only affects type checking during compilation.
This feature would agree with the rest of TypeScript's Design Goals.
While this feature agrees with all of the design goals, it fits most neatly under goals 5 and 9: "Produce a language that is composable and easy to reason about." and "Use a consistent, fully erasable, structural type system."
β Suggestion
It is currently possible to assign a
this
parameter to a method to ensure that it is used correctly. For example:This capability is enormously useful when defining objects that will be used to extend other types.
However, there is a glaring missing feature. While it is possible to add property accessors to the extension object, they do not have access to the
this
reference, even though the getter and setter accessors are fundamentally just theget
andset
functions on the property descriptor. This is severely limiting, even though it is fully supported in JavaScript.π Motivating Example
Consider the following simple extension framework code. It uses
getOwnPropertyDescriptor
anddefineProperty
, which preserves the original accessor functions (the getter/setter). JavaScript evaluates thethis
references in the accessors in the context of the target object, allowing any property getter/setter logic to behave as if the properties were defined on the target from the beginning.TypeScript's support for
this
on property accessors would allow extension framework authors to express this powerful feature of JavaScript and ensure correctness in their code.In addition to the convenience of accessing the intended
this
reference, TypeScript could use the same kind of type safety for properties that it currently offers for functions.Checking the intended
this
reference for property accessors could be used to help prevent common mistakes, such as the following:Here, the engineer intended to attach the
bar
property accessor functions but accidentally triggered the evaluation of the property value instead. Instead of assigning a dynamic getter, the target received a new property with a simple, fixed value (or a potential runtime error).A playground link which demonstrates the proposed code (and actually runs, despite the TS error!)
π» Use Cases
The use cases for the
this
parameter in property accessors includes all the use cases for thethis
parameter in functions, including:...and any other code where a property accessor defined on one object will ultimately be used by another.
The text was updated successfully, but these errors were encountered: