question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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: 1

But context passed to the nested function is lost: 4

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:open
  • Created 6 years ago
  • Reactions:13
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

155reactions
neotancommented, Feb 25, 2020

I simply tested noImplicitThis: false in TS playground, it seems work

// tsconfig.js

     "noImplicitThis": false,                /* Raise error on 'this' expressions with an implied 'any' type. */

30reactions
andy-mscommented, Nov 1, 2017

It’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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found