typescript-eslint: Bug: [no-invalid-void-type] Can't use `void` in return type union for overloaded function

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

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 24 (14 by maintainers)

Commits related to this issue

Most upvoted comments

But if it’s valid in the overloading case, it should also be valid without overloading.

I disagree.

If you’re declaring a function with an overload you’re explicitly declaring a case in which a function return can be ignored (eg some combination of parameters).

Whilst an overload does ultimately result in a function implementation that is declared with a void union (if you’re explicitly typing the return), it is is vastly different in style and intetion to just declaring any old function with a void union.

Put another way with an overload you’re saying “in this specific case the function will return nothing, and in this other case it returns something” whereas in the non-overload case you’re saying “this function might return nothing or something” - vastly different meanings!

Normally the void | string return type would be a direct “no” from this rule…

Why is that? I think the following should be allowed:

function g(x?: string): void | string {
  if (x !== undefined && x.length > 10) {
    return x;
  }
}

It’s identical to the function overloading case: either you keep void in return type, or you change void to undefined and manually return undefined in the end of the function.

What about this case:

const foo = new Subject<void | string>();

But if it’s valid in the overloading case, it should also be valid without overloading. I think it’s not consistent if you disallow void for common functions but allow it for overloaded functions.

While this is also related to https://github.com/typescript-eslint/typescript-eslint/issues/8113, I think this can progress separately. We just need to ignore the implementation signature if overload signatures exist.

Btw, your code at runtime has an unhandled case because x := 0 means x is 0 in the else branch

That is incorrect. x != null does not treat falsey values as false. It returns false if and only if x is null | undefined - i.e. it is equivalent to x !== null && x !== undefined.


I would like to be clear here:

This issue has nothing at all to do with the general case of | void in return types. That is a topic for a separate issue. This issue is specifically for allowing | void in the return type of a function that has an overloaded declaration that returns a bare void.

We are accepting a PR to support the latter.

The former is not up for debate in this issue and is off-topic. If the former is something that interests you - please file a separate issue.

Clarifying question: what is “UB”?

Undefined Behavior

I think void unions should always be allowed in return types (even without overloads), as far as I know it is the only way to say that a function should either return a specific type or return nothing, which I think is a valid use case.

type ReturnNothingOrNumber = () => void | number;

// Should work
const f1: ReturnNothingOrNumber = () => {};

// Should work
const f2: ReturnNothingOrNumber = () => {
    return 42;
};

// Should *not* work
const f3: ReturnNothingOrNumber = () => {
    return "42";
};

With () => undefined | number, the first function does not typecheck (type void is not assignable to type undefined | number).

I’ve stumbled upon this issue working on a wrapper function around immer. The recipe argument to the produce function in immer is a function that should either not return anything (most common case), or return something of a specific type.

Interesting. Normally the void | string return type would be a direct “no” from this rule… but it’s also the only way to support overloads. Accepting PRs for this case!