Skip to content
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

Check inferred this parameter against interface requirement this: void #44513

Open
jtbandes opened this issue Jun 8, 2021 · 5 comments
Open
Assignees
Labels
Rescheduled This issue was previously scheduled to an earlier milestone Suggestion An idea for TypeScript

Comments

@jtbandes
Copy link
Contributor

jtbandes commented Jun 8, 2021

Bug Report

πŸ”Ž Search Terms

interface method requirement this: void

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

interface Foo {
  method(this: void): number;
}
class Example implements Foo {
  x = 3;
  method() { return this.x; } // no error
}

πŸ™ Actual behavior

No error

πŸ™‚ Expected behavior

Error because the method implementation's implicit this: Example is not compatible with the interface requirement this: void.

The @typescript-eslint/unbound-method lint rule uses this: void as an annotation indicating the method is allowed to be used without binding this. However, there seems to be no checking from the TS compiler as to whether an inferred this type in a method implementation matches an interface requirement. Thus the lint rule can help you add this: void to the interface if you intend for functions to be usable when not bound... but neither the linter nor compiler currently help prevent runtime errors resulting from implementations with mismatched this types.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Jun 15, 2021
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.4.1 (RC) milestone Jun 15, 2021
@sandersn sandersn added this to Not started in Rolling Work Tracking via automation Jun 15, 2021
@sandersn
Copy link
Member

As I recall, the implicit this for methods is still this: any not this: this, which was an intentional decision for backward compatibility. It might be time to revisit this decision. I'll investigate during the 4.4 beta.

@fatcerberus
Copy link

IIRC, I think I recall @RyanCavanaugh saying the reason for that is because this: this being the default implicitly makes all methods generic, which was a performance concern.

@fatcerberus
Copy link

Found it: #35451 (comment)

@andrewbranch andrewbranch added the Rescheduled This issue was previously scheduled to an earlier milestone label Aug 30, 2021
@sandersn
Copy link
Member

From my memory of method instantiation, that's not true; classes are already all generic, so methods shouldn't need an additional type parameter (or instantiation) to use the classes' this.

That should be the first thing I investigate because I could easily be wrong.

@sandersn
Copy link
Member

sandersn commented Nov 1, 2021

Yes, the only difference between this: Example and this: this in the above example is which type you get. The checker creates this<Example> at the same time it creates Example.

The next question is what breaks when methods have this: this by default.

Edit: No, I am wrong. The number of types is the same, but the method symbol also gets instantiated when there's a usage of a this type. It might be worth seeing how bad performance is nonetheless.

@sandersn sandersn added Suggestion An idea for TypeScript and removed Needs Investigation This issue needs a team member to investigate its status. labels Nov 3, 2021
@sandersn sandersn removed this from the TypeScript 4.5.0 milestone Nov 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Rescheduled This issue was previously scheduled to an earlier milestone Suggestion An idea for TypeScript
Projects
Rolling Work Tracking
  
Not started
Development

No branches or pull requests

5 participants