Skip to content

Polymorphic this behavior after method extraction #45781

Closed
@Guerric-P

Description

@Guerric-P

Bug Report

After extracting a method from a class prototype (which becomes a plain function), the polymorphic this still behaves like the function is attached to the class prototype.

🔎 Search Terms

polymorphic this

🕗 Version & Regression Information

I saw this happening in the latest current version (4.4.2) and I don't know since when the bug exists.

⏯ Playground Link

Playground link with relevant code

💻 Code

class Foo {
  axisControl: string = "string";
  directionControl: string = "string";

  filteredAxis: string[] = ["string"];
  filteredDirection: string[] = ["string"];

  test(filteredName: `filtered${string}` & keyof this) {
    return this[filteredName];
  }
}

const foo = new Foo;
const result = foo.test("filteredAxis"); // Type resolves here: string[]

const test = Foo.prototype.test;
const wrong = test("filteredAxis"); // It also resolves here to string[]

const bound = test.bind(undefined);
const wrong2 = bound("filteredAxis"); // It also resolves here to string[]

const wrong3 = Foo.prototype.test.apply(undefined, ["filteredAxis"]); // It also resolves here to string[]

const wrong4 = Foo.prototype.test.call(undefined, "filteredAxis"); // It also resolves here to string[]

🙁 Actual behavior

The wrong, wrong2, wrong3, wrong4 variables types resolves to string[]

🙂 Expected behavior

Compilation errors to prevent the TypeError: Cannot read properties of undefined runtime errors

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