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

Skip to content

Bug: [no-invalid-void-type] Can't use void in return type union for overloaded function #5752

@haines

Description

@haines

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&sourceType=module&code=GYVwdgxgLglg9mABMAFASgFyIG5xgEwG4AoUSWBZFADywGcoAnGMAc00QebZLOniSpqAfnpMW7LLgKIAPp3FtEAb2KJEMYIhqIAhAF59icPgCmwFqfxoVa9YkamoIRkmol1AX2LfS4fpSs6FgACoxwALYwdKYAPNL4AHy8-hRIQbQK3JKIYZHRcVwSycQAhnQAnpDIqQKIGaJZEhx5UTHxePhyTWyJtuqa2tR6hsZgZhZgVjaq9g5OLm4eiN6eQA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1uYDcBDRgBNaPDpWFFS6KImjQO0SODABfECqA&tsconfig=N4XyA

Repro Code

function f(): void;
function f(x: string): string;
function f(x?: string): void | string {
  if (x !== undefined) {
    return x;
  }
}

function g(): Promise<void>;
function g(x: string): Promise<string>;
async function g(x?: string): Promise<void | string> {
  if (x !== undefined) {
    return x;
  }
}

ESLint Config

module.exports = {
  "rules": {
    "@typescript-eslint/no-invalid-void-type": "error"
  }
}

tsconfig

{}

Expected Result

No errors.

Actual Result

void is only valid as a return type or generic type variable. 3:25 - 3:29
void is only valid as a return type or generic type variable. 11:39 - 11:43

Additional Info

It's possible to work around the linter in the first example by changing the implementation return type from void | ... to undefined | ..., although this may require introducing an explicit return undefined statement to the function body if the compiler option noImplicitReturns is set.

In the second example, changing Promise<void | ...> to Promise<undefined | ...> is not possible because it introduces a compiler error

This overload signature is not compatible with its implementation signature. (2394)

Versions

package version
@typescript-eslint/eslint-plugin 5.39.0
@typescript-eslint/parser 5.39.0
TypeScript 4.8.4
ESLint 8.15.0
node web

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workinglocked 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

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions