Skip to content

Polymorphic this behavior after method extraction #45781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Guerric-P opened this issue Sep 8, 2021 · 3 comments
Closed

Polymorphic this behavior after method extraction #45781

Guerric-P opened this issue Sep 8, 2021 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@Guerric-P
Copy link
Contributor

Guerric-P commented Sep 8, 2021

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

@andrewbranch
Copy link
Member

Note that you can opt into enforcing this by explicitly annotating a this type for the method. I know I’ve heard some team members express that they wish we did this automatically for class methods, but by now it would probably be too big a breaking change. I feel confident there must be an issue discussing this as a suggestion, but I’m having trouble finding it.

@andrewbranch andrewbranch added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript and removed Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Sep 8, 2021
@andrewbranch
Copy link
Member

Just had to be extra clever with GitHub search. Duplicate of #7968.

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Sep 8, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants