Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[unbound-method] Should prevent conforming to a bound-style interface method with an unbound method #3503

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

Open
3 tasks done
jtbandes opened this issue Jun 8, 2021 · 1 comment
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@jtbandes
Copy link
Contributor

jtbandes commented Jun 8, 2021

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

interface Foo {
  unbound(): number;
  unboundOk(this: void): number;
  bound: () => number;
}

function what(f: Foo) {
  const unbound = f.unbound;      // lint error πŸ‘
  const unboundOk = f.unboundOk;  // no error πŸ‘
  const bound = f.bound;          // no error πŸ‘

  // console.log(unbound());  // runtime error (expected)
  console.log(unboundOk()); // runtime error ❌
  console.log(bound());     // runtime error ❌
}

class Example implements Foo {
  x = 3;
  unbound() { return this.x; }
  unboundOk() { return this.x; }   // no error πŸ‘Ž (TypeScript issue rather than typescript-eslint issue?)
  bound() { return this.x; }      // no error πŸ‘Ž (this issue)
}
what(new Example());

eslint config: https://github.com/foxglove/eslint-plugin/blob/main/configs/typescript.js
tsconfig: https://github.com/foxglove/studio/blob/main/packages/tsconfig/tsconfig.base.json

Expected Result

class Example implements Foo is problematic. There should be an error on bound() { return this.x } when is used to satisfy the interface requirement bound: () => number;.

Actual Result

The lint rule does not reject this incorrect method implementation, which leads to a runtime error.

Versions

package version
@typescript-eslint/eslint-plugin 4.26.0
@typescript-eslint/parser 4.26.0
TypeScript 4.3.2
ESLint 7.27.0
node 15.9.0
@jtbandes jtbandes added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Jun 8, 2021
@jtbandes jtbandes changed the title [unbound-method] Should prevent conforming to an bound-style interface method with an unbound method [unbound-method] Should prevent conforming to a bound-style interface method with an unbound method Jun 8, 2021
@bradzacher bradzacher added enhancement: plugin rule option New rule option for an existing eslint-plugin rule and removed triage Waiting for team members to take a look labels Jun 8, 2021
@bradzacher
Copy link
Member

It's important to note that you are misunderstanding what TS syntax means.

interface Foo {
  prop: () => number;
}

This does not mean or enforce that prop is an arrow function.
Whilst it looks similar to an arrow function property - the two are not at all related!

The difference between prop: () => number and prop(): number is only apparent when the strictFunctionTypes flag is turned on. You can read more in our method-signature-style rule.
Otherwise though - a method signature and a property signature are just different syntax for the same thing.

The reason that unbound-method does not report on bound is simply because it does not have support in the rule for detecting "property signatures with function types".

@JoshuaKGoldberg JoshuaKGoldberg added the accepting prs Go ahead, send a pull request that resolves this issue label Oct 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

3 participants