thisAliases
Reports assigning
thisto a variable.
✅ This rule is included in the ts logical presets.
Before ES2015, a common pattern was to assign this to a variable (often named self, that, or _this) to preserve the context when using function expressions.
With arrow functions, this pattern is no longer necessary because arrow functions automatically preserve the surrounding this context.
Examples
Section titled “Examples”class Example { value = 10;
method() { const self = this; setTimeout(function () { console.log(self.value); }, 100); }}class Example { value = 10;
method() { setTimeout(() => { console.log(this.value); }, 100); }}Destructuring is Allowed
Section titled “Destructuring is Allowed”Destructuring properties from this is allowed because it’s extracting specific values, not aliasing the context:
class Component { props = { title: "Hello" }; state = { count: 0 };
render() { const { props, state } = this; return `${props.title}: ${state.count}`; }}Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you’re maintaining legacy code that heavily uses this aliasing and refactoring to arrow functions would be too costly, you might need to disable this rule.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”
Made with ❤️🔥 around the world by
the Flint team and contributors.