You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Search Terms: bound unbound callback this parameter
Code
classC{publicprop=1;}constc=newC();functioncallUnbound(fn: ()=>void): void{fn();}functioncallBound(fn: (this: C)=>void): void{//fn(); // compiler error as expectedfn.call(c);// passes as expectedcallUnbound(fn);// passes unexpectedly (`callUnbound` does not call with context)}callBound(function(){console.log(this.prop);});
Expected behavior:
Last line in callBound causes a compiler error.
Actual behavior:
Last line in callBound passes the compiler, and fn will be called without this context.
Playground Link:
Related Issues:
The text was updated successfully, but these errors were encountered:
The callBound() call on the last line likely wouldn't be an error even with stricter this typing, because its argument function has no this annotation at all. It's the same principle that you can pass a function with fewer parameters in place of one with more. The callUnbound() call is where I would expect the compiler error, if one were to be produced.
edit: Disregard, I was stupid and misread the issue.
The callBound() call on the last line likely wouldn't be an error even with stricter this typing, because its argument function has no this annotation at all. It's the same principle that you can pass a function with fewer parameters in place of one with more. The callUnbound() call is where I would expect the compiler error, if one were to be produced.
That's what I wanted to express with "Last line in (the function) callBound". :)
TypeScript Version: 3.7.2
Search Terms: bound unbound callback this parameter
Code
Expected behavior:
Last line in
callBound
causes a compiler error.Actual behavior:
Last line in
callBound
passes the compiler, andfn
will be called withoutthis
context.Playground Link:
Related Issues:
The text was updated successfully, but these errors were encountered: