'this' implicitly has type 'any' because it does not have a type annotation

后端 未结 1 1799
执笔经年
执笔经年 2020-12-12 18:46

When I enable noImplicitThis in tsconfig.json, I get this error for the following code:

\'this\' implicitly has type \         


        
相关标签:
1条回答
  • 2020-12-12 19:22

    The error is indeed fixed by inserting this with a type annotation as the first callback parameter. My attempt to do that was botched by simultaneously changing the callback into an arrow-function:

    foo.on('error', (this: Foo, err: any) => { // DON'T DO THIS
    

    It should've been this:

    foo.on('error', function(this: Foo, err: any) {
    

    or this:

    foo.on('error', function(this: typeof foo, err: any) {
    

    A GitHub issue was created to improve the compiler's error message and highlight the actual grammar error with this and arrow-functions.

    0 讨论(0)
提交回复
热议问题