-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Feature-Request: Support an autobinding-attribute for this in class methods #18526
Comments
Unfortunately this isn't in our scope - check out non-goals for the project:
TypeScript likely won't ever provide decorators or any other code that another library could provide instead. |
Stupid. |
I'm sorry if I've upset you, but that sort of discourse isn't really helpful and isn't welcome here. Please keep our code of conduct in mind when participating in our repo. |
Just to clariy: Now, if this is solved via decorater, keyword, command-line-switch/option or whatever, I really don't care. All I care about is that it's very error-prone to have to do it manually in every constructor of every class for every method, including every change anybody makes to it, and therefore it shouldn't be done manually - ever. Having policies/guidelines that will lead to requiring mobile users to download 100 kb of additional superfluous javascript is stupid, and a great way to to make your websites load slow in no time, especially if this code-bloat occurs multiple-times. Also, it's a great way to destroy the web for millions of users with slow internet connections and/or metered connections. As is requiring dependencies to 2 additional external libraries for something that can/could be done at compile-time. But have it your way. |
Binding of methods is very specific to their use case. It is always better/more efficient to not bind unless you have to (as discussed by Ryan in this comment). You seem to imply there is a one size that fits all. You also argue for a decorator that could easily be written by yourself and applied as you see fit. Given the narrow use case (a method in a class that access Good luck with Babel. |
If the difference between class C {
fn() {
}
} and class C {
fn = () => {
}
} is too much to manage, I'm not sure Babel will solve your problems either. |
@RyanCavanaugh: Thank you very much for your very nice comment, but I'm actually aware what arrow-functions are. Oh, and by the way: Try call a base-class method from a derived class using super if you use arrow-functions. Good luck, because it isn't possible. |
👍 your own comments and then using a shadow account to reinforce your viewpoint... priceless... |
Suppose in a typescript-class you have this method:
Now, if you pass that function into a callback, it will return undefined.
As the C# example below shows, this is not what you'd get in a proper programming language.
Now I understand "this" in JavaScript has different semantics.
But since TypeScript requires writing this.m_Length, etc, it should use the correct this.
There is a suggested work-around in
https://daveceddia.com/avoid-bind-when-passing-props/
Rather than using arrow-functions (which don't always work, btw), one could bind the method in the constructor, the methods should be bound to "this" in the constructor.
It would be nice if TypeScript supported an "attribute" like @autoBind out of the box.
Because right now, I always have to remember to add the constructor-binding for every method I add.
This is very error prone.
An attribute that would instruct the transpiler to do this "automagically" at compile-time would be better.
But I would prefer not to have to import a module at runtime, like described by Dave Ceddia.
import autobind from 'autobind-decorator';
I would prefer if this were done at compile/transpile time.
Maybe in addition to that, it would be nice to have another attribute like @ignore_autobind, which could be set at an individual method that should not be auto-bound.
In addition, this should also support properities.
Because of the way typescript transpiles, I can't manually add a constructor-binding to "this" for a PROPERTY.
Doing constructor-binding would have the additional advantage over arrow-functions in that it isn't 30% slower.
Same/similar thing in C# works correctly, since "this" works as a instance-of-class-reference, and not an "arbitrary" context.
The text was updated successfully, but these errors were encountered: