no-invalid-this
Disallow
this
keywords outside of classes or class-like objects.
danger
The code problem checked by this ESLint rule is automatically checked by the TypeScript compiler. Thus, it is not recommended to turn on this rule in new TypeScript projects. You only need to enable this rule if you prefer the ESLint error messages over the TypeScript compiler error messages.
(Note that technically, TypeScript will only catch this if you have the strict
or noImplicitThis
flags enabled. These are enabled in most TypeScript projects, since they are considered to be best practice.)
This rule extends the base eslint/no-invalid-this
rule.
It adds support for TypeScript's this
parameters.
Options
See eslint/no-invalid-this
's options.
How to Use
.eslintrc.cjs
module.exports = {
"rules": {
// Note: you must disable the base rule as it can report incorrect errors
"no-invalid-this": "off",
"@typescript-eslint/no-invalid-this": "error"
}
};
Try this rule in the playground ↗