Skip to content

Cannot access variant accessors from other class method with this contextΒ #43443

Closed
@whzx5byb

Description

@whzx5byb

Bug Report

πŸ”Ž Search Terms

πŸ•— Version & Regression Information

  • This changed between versions 4.2.3 and 4.3.0-dev.20210330

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

class A {
  private _x: number = 0;

  get x() {
    return this._x;
  }

  protected set x(v: number) {
    this._x = v;
  }
}

class B {
  foo(this: A) {
    this.x = 1; // Property 'x' is protected and only accessible through an instance of class 'B'. This is an instance of class 'A'.(2446)
  }
}

function bar(this: A) {
  this.x = 1; // This is OK.
}

πŸ™ Actual behavior

The behavior of a protected setter is influenced by the accessibility of getter.
If you change the modifier of get x() to protected, the error will go away.

Also the error message is unclear. It says Property 'x' is protected and only accessible through an instance of class 'B', but x is only accessible through an instance of 'A', not 'B'.

πŸ™‚ Expected behavior

No error.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions