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

Open
@jtbandes

Description

@jtbandes
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issueenhancement: plugin rule optionNew rule option for an existing eslint-plugin rulepackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions