Skip to content

Enhancement: [no-misused-promises] Check when an abstract method implementation returns a promise instead of void #8739

Closed
@alythobani

Description

@alythobani

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=4.8.4&showAST=es&fileType=.tsx&code=FBA&eslintrc=N4KABGBEBOCuA2BTAzpAXGYBfEWg&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

abstract class A {
  thing: number | null = null;

  abstract setThing(): void;

  setAndPrintThing(): void {
    this.setThing();
    console.log(this.thing);
  }
}

class B extends A {
  // Expected no-misused-promises error here
  async setThing(): Promise<void> {
    const thing = await fetchThing();
    this.thing = thing;
  }
}

async function fetchThing(): Promise<number> {
  return await new Promise<number>((resolve) => {
    setTimeout(() => {
      resolve(5);
    }, 1000);
  });
}

const b = new B();

// Prints null instead of 5
b.setAndPrintThing();

function _setAndPrintThing(setThing: () => void): void {
  setThing();
  console.log(b.thing);
}

// Correctly triggers no-misused-promises error:
//  "Promise returned in function argument where a void return was expected"
_setAndPrintThing(b.setThing.bind(this));

ESLint Config

{
  "rules": {
    "@typescript-eslint/no-misused-promises": "error"
  }
}

tsconfig

{
  "compilerOptions": {
    "strictNullChecks": true
  }
}

Expected Result

Line 14 (where B.setThing is implemented) should show an error like "Promise returned in abstract method implementation where a void return was expected"

Actual Result

There's no error on line 14

Additional Info

I'm not sure why the playground link doesn't seem to be working, but hopefully you can see my code sample anyway and it makes sense.

Based on the no-misused-promises page it looks like maybe this should be another category under checksVoidReturn?

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 rulelocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: 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