oxc-project/oxc · error · OxcDiagnostic

Unexpected aliasing of 'this' to local variable.

Error message

Unexpected aliasing of 'this' to local variable.

What it means

Raised by typescript/no_this_alias when `this` is assigned to a local variable (e.g. `const self = this`). At the throw site the alias exists to preserve `this` inside a nested regular function — a pre-arrow-function workaround; in TypeScript/ES2015+ an arrow function preserves lexical `this` and removes the need for the alias, which also defeats some type narrowing. no_this_alias_diagnostic fires from run() on assignment targets bound to the ThisExpression.

Source

Thrown at crates/oxc_linter/src/rules/typescript/no_this_alias.rs:20

    AstKind,
    ast::{AssignmentTarget, BindingPattern, Expression, match_simple_assignment_target},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use oxc_str::CompactStr;
use rustc_hash::FxHashSet;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::{
    AstNode,
    context::{ContextHost, LintContext},
    rule::{DefaultRuleConfig, Rule},
};

fn no_this_alias_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Unexpected aliasing of 'this' to local variable.")
        .with_help("Assigning a variable to this instead of properly using arrow lambdas may be a symptom of pre-ES2015 practices or not managing scope well.")
        .with_label(span)
}

fn no_this_destructure_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Unexpected aliasing of members of 'this' to local variables.")
        .with_help(
            "Disabling destructuring of this is not a default, consider allowing destructuring",
        )
        .with_label(span)
}

#[derive(Debug, Default, Clone, Deserialize)]
pub struct NoThisAlias(Box<NoThisAliasConfig>);

#[derive(Debug, Clone, JsonSchema, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
pub struct NoThisAliasConfig {

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Replace the alias with an arrow function to preserve `this`.
  2. Use function.bind(this) where a callback needs the this value.
  3. Add an allowDestructuring/allowedNames exception if the alias is deliberate.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/typescript/no_this_alias.rs:20 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/c47b54e4669d8945. Report an issue: GitHub.