- Error
- Reported by the type checker
TS2683
'this' implicitly has type 'any' because it does not have a type annotation.
this is used in a function whose this type is unknown, under noImplicitAny. Use an arrow function, a method, or declare a this parameter.
What it means
Inside a plain function, this depends on how the function is called. When the checker can't tell its type from context and noImplicitAny (part of strict) is on, using this is an error.
Example
function getName() {
return this.name;
}
export {};
ts-r-check -p tsconfig.json --pretty false ts-r outputindex.ts(2,10): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
Recorded by node scripts/verify-examples.mjs with ts-r built from commit 6deaf01. The example uses the default tsconfig: strict, target: es2022, module: esnext, moduleResolution: bundler.
How to fix
- Declare the expected type with a
thisparameter:function getName(this: { name: string }). - Use an arrow function to capture the outer
this, or make it a method of a class or object literal.