Error "'this' implicitly has type 'any'" when used .bind()
See original GitHub issue› tsc --version
Version 2.7.0-dev.20171020
Code
tsconfig.json:
{
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"noEmit": true,
"strict": true
},
"files": [
"index.d.ts",
"index.js"
]
}
index.js:
/** @type {MyObj} */
const o = {
foo: function() {
(function() {
console.log(this); // <- Unexpected error here.
}.bind(this))();
}
};
index.d.ts:
interface MyObj {
foo(this: { a: number }): void;
}
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:
› tsc
index.js(5,25): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:13
- Comments:9 (3 by maintainers)
Top Results From Across the Web
Error "'this' implicitly has type 'any'" when used .bind() #19639
There should not be error, because this explicitly specified by .bind() . Actual behavior: › tsc index.js(5,25): error TS2683: 'this' ...
Read more >'this' implicitly has type 'any' because it does not have a type ...
The error is indeed fixed by inserting this with a type annotation as the first callback parameter. My attempt to do that was...
Read more >Binding element 'X' implicitly has an 'any' type in TS | bobbyhadz
The error "Binding element implicitly has an 'any' type" occurs when we don't set the type of an object parameter in a function....
Read more >'this' implicitly has type 'any' because it does not ... - You.com
The error is indeed fixed by inserting this with a type annotation as the first ... because you are using the function ()...
Read more >TypeScript errors and how to fix them
You need to assign your type declaration using the = character: ... get fullName() { ... TS1192. error TS1192: Module ' json5 '...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I simply tested
noImplicitThis: false
in TS playground, it seems workIt’s possible that we could detect this pattern like we detect IIFEs. A workaround for now might be to use
const that = this;
instead if your runtime doesn’t yet support arrow functions.