no-invalid-this
Disallow
this
keywords outside of classes or class-like objects.
This is an "extension" rule that replaces a core ESLint rule to work with TypeScript. See Rules > Extension Rules.
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 no-invalid-this
rule from ESLint core. It adds support for TypeScript's this
parameters.
Options
See eslint/no-invalid-this
's options.
How to Use
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
// Note: you must disable the base rule as it can report incorrect errors
"no-invalid-this": "off",
"@typescript-eslint/no-invalid-this": "error"
}
});
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 ↗