Skip to content

Static getters / setters can have incorrect this binding  #4327

@matthewjh

Description

@matthewjh
class Base {
  static getCorrectFoo(): any {
     return this;
  }

   static get foo(): any {
      return this;
   }
}

class Inherited extends Base {}
Base.foo === Base;
Base.getCorrectFoo() === Base;

Inherited.foo === Base; /// Should be `Inherited`
Inherited.getCorrectFoo() === Inherited;

I believe this is because the foo getter will be defined via Object.defineProperty(Base, {..}), which hard-wires this to Base within the getter, rather than binding as expected to Inherited in Inherited.foo.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions