Skip to content
This repository has been archived by the owner on Jan 19, 2019. It is now read-only.

Latest commit

 

History

History
60 lines (45 loc) · 1.23 KB

no-this-alias.md

File metadata and controls

60 lines (45 loc) · 1.23 KB

Disallow aliasing this (no-this-alias)

This rule prohibts assigning variables to this.

Rule Details

Rationale from TSLint:

Assigning a variable to this instead of properly using arrow lambdas may be a symptom of pre-ES6 practices or not managing scope well.

Instead of storing a reference to this and using it inside a function () {:

const self = this;

setTimeout(function() {
    self.doWork();
});

Use () => arrow lambdas, as they preserve this scope for you:

setTimeout(() => {
    this.doWork();
});

Examples of incorrect code for this rule:

(see the rationale above)

Examples of correct code for this rule:

(see the rationale above)

Options

You can pass an object option:

{
    "typescript/no-this-alias": [
        "error",
        {
            allowDestructuring: true, // Allow `const { props, state } = this`; false by default
            allowedNames: ["self"], // Allow `const self = this`; `[]` by default
        },
    ],
}

When Not To Use It

If you need to assign this to variables, you shouldn’t use this rule.

Related to