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
Suggestion when non-bound or non-arrow function is passed to another function. #31684
Comments
Wouldn't this be a breaking change in existing TypeScript code (unless it's behind an opt-in compiler flag)? |
From what I understand, the default for functions without an explicit It might be nice to have a flag to make the behavior stricter, e.g. something like edit: So this is a way you can get the type checker to help you out here: class Foo {
on(event: 'click', callback: (this: void) => void) {
callback();
}
}
class Bar {
handleClick(this: this) {}
blub() {
const foo = new Foo();
foo.on('click', this.handleClick); // error - 'void' is not assignable to 'this'
}
} You need to both declare the method as |
Related to this I believe #7968 |
It would be nice to have a more seamless experience. A Maybe the compiler could even suggest a fix when used incorrectly (class properties: This is a class of errors that is currently uncaught. I see a lot of people struggling with this (no pun intended). It's now even more common with native web components and libraries like lit element |
We tried having this as a behavior, but the implementation effectively makes every class method in the world generic, and the performance implications were beyond what anyone would consider acceptable. The current state of things under |
Can you explain what you mean by this? Class methods without a Performance implications I understand, but "every method in the world is generic" is the situation we already have today. |
By "generic" I mean "having a type parameter" and thus subject to more complex rules around assignability |
I still don't understand - I'm probably missing something really obvious here... |
Unless you mean specifically |
Bingo |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Would it still be possible to generate |
You can opt into it using an annotation directly on the method: class MyClass {
m(this: this) {
}
}
function onFoo(cb: (this: void) => void) {
cb();
}
onFoo(new MyClass().m) //Error
let o = new MyClass()
onFoo(o.m.bind(o)) // ok an well typed under strictBindCallApply Not sure how useful this is since you have to do it on the method but though it is worth mentioning. |
Search Terms
related #10285
#15
Suggestion
When we pass a function to external code in our class, f.e.
it may be nice for TypeScript to provide a warning or error if the function being passed is not bound to
this
or is not an arrow function closing the samethis
.I don't see any warning or error, despite the status of #15. Maybe I missed something?
Use Cases
It can prevent runtime errors.
Examples
See above.
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: