-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Error "'this' implicitly has type 'any'" when used .bind() #19639
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
It's possible that we could detect this pattern like we detect IIFEs. |
The same situations with context in loops like /** @type {MyObj} */
const o = {
foo: function() {
[].forEach(function() {
console.log(this); // <- Unexpected error here.
}, this);
}
}; |
Support for that was removed in #16223 for performance reasons. |
@offg777 That depends on the enclosing scope. Where do you expect |
I think I understand now, I'm new to React. Thanks for the clarification. |
This has got to be a bug. I can copy and paste insurmountable different, fully-tested functions & function-constructors into a TS file only to have a ton of work ahead of me just to satisfy TS. That's not necessarily nonsensical, but if TS exists because its intention is to be compiled to JS, then why should it matter if function Methods() {
...
this.method = (...) => {...};
...
return this;
}
var $ = Methods.call(function jQuery(...) {...});
$(...).method(...); I feel like this whole Is there a Type Declaration workaround for this problem? I need to be able to use Function Constructors at times. At the moment, |
I simply tested // tsconfig.js
"noImplicitThis": false, /* Raise error on 'this' expressions with an implied 'any' type. */ |
That is certainly a strong point of JavaScript. But also a strong concern point for some. If you wish to leverage those powers you have in JavaScript, then TypeScript is definitely not a good match for your needs. JavaScript is. TypeScript is specificly for restricting what you can do with JavaScript. I also got blocked by this limitation, and I had to go with @neotan 's suggestion. It worked for my need. My use case is a (generic) debounce function, where it needs to hold a reference to I would appreciate if there is a way around this without disabling this check completely. Although I couldn't think of any easy way of this being possible. |
my solutionfix: TypeScript & ES5 function
const nums = [1, 2, 3];
nums.myForEach(function(a, b, c, thisObj) {
console.log(`a, b, c =`, a, b, c);
console.log(`thisObj =`, thisObj);
console.log(`this = `, this);
});
const nums = [1, 2, 3];
nums.myForEach(function(this: any, a, b, c, thisObj) {
// A 'this' parameter must be the first parameter.ts(2680) ✅
console.log(`a, b, c =`, a, b, c);
console.log(`thisObj =`, thisObj);
console.log(`this = `, this);
}); test ✅ |
Code
tsconfig.json:
index.js:
index.d.ts:
How it looks in the editor:
Context of

foo()
is defined:But context passed to the nested function is lost:

Expected behavior:
There should not be error, because
this
explicitly specified by.bind()
.Actual behavior:
The text was updated successfully, but these errors were encountered: