The error "this implicitly has type any" occurs when we use the this keyword outside of classes or in functions where the type of this cannot be inferred. To solve the error, add a type for the this keyword as the first parameter in the function. Copied!
TS7011: Function expression, which lacks return-type annotation, implicitly has an 'any' return type. I understand it can be fixed by adding : void return type or removing void operation or removing noImplicitAny : window.
To define the return type for the function, we have to use the ':' symbol just after the parameter of the function and before the body of the function in TypeScript. The function body's return value should match with the function return type; otherwise, we will have a compile-time error in our code.
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.
For method decorator declaration
with configuration "noImplicitAny": true,
you can specify type of this variable explicitly depends on @tony19's answer
function logParameter(this:any, target: Object, propertyName: string) {
//...
}
you can add
"noImplicitAny": false,
to
tsconfig.json
as is says here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With