-
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
The compiler should error when unbinding a method #25647
Comments
Duplicate #15 You can do this today by annotating sufficiently: class A {
x = 3;
foo(this: A) {
this.x = 10;
}
}
function f(y: (this: void) => void) { }
const a = new A();
// Error
f(a.foo); |
Didn't find that issue - thanks. But that issue was closed with the introduction of the So far the Yes, you can annotate it like that, but its not feasible to do so for every method. When the types come from a library you usually don't have control over it. I propose that the containing class/interface becomes the Casting can always be used as an escape hatch. |
We tried to detect this globally, but the performance implications were too much (> 15% hit to compilations that use classes at all). There are also more subtle design issues that are apparent at first glance -- many class methods don't actually use Between the perf hit and the imprecision, we decided the current solution of opting-in with annotation was the best compromise. |
Okay, didn't know this would have perf implications. Curious, what makes it so complex? What part do you mean with "detect this globally"? Checking for methods that are assigned/passed and therefor unbound?
I would argue you still shouldn't unbind it. Just because a method doesn't use
In that case the types for that method could explicitly declare that by using
I have no problem with it being opt-in, but I currently don't see a practical way to opt in as a user. The only ones who can opt-in are library/types authors. There is some mismatch right now because even when not explicitely declaring But imo it makes way more sense to have a mode where it makes |
#10288 tracks this now. |
So I just tried to hack around in TSLint's |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
Search Terms
unbound method
bind
Suggestion
When referencing a method like this:
the compiler should error.
I imagine this would work something like that the
this
type of any method is its class by default, i.e.next(this: Subject)
here.Assigning or passing a method somewhere means that at runtime the function is "casted" to
next(this: undefined)
, therefor the compiler should check if that "cast" is valid by checking ifSubject
is assignable toundefined
, and emitting an error.To make the error go away, one would have to bind it:
A simplified
bindThis()
that only takes care ofthis
and does no partial application would be defined as (with tuple types):An arrow function's
this
type would always benever
, so wrapping it works too.We could then also type lodash's
bindAll()
:(the method whitelist would be easy to type too)
Use Cases
Currently when using RxJS in React components, its extremely cumbersome to convert callback or lifecycle events to Subjects. You end up with writing a bound version for every
next()
function of every Subject:just so you can e.g. pass
nextClick
toonClick
in therender
function. If I also need to callerror
, I need to bind that too.There are even packages like https://www.npmjs.com/package/bound-subject-decorator to solve it, but there is no way to make this type safe in a way that the compiler will tell you when you forgot to use it.
With this proposal, I could just wrap the subject in
bindAll()
.TSLint has a
no-unbound-method
rule but it is not very smart. This would better come from the type checker itself.Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: