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
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
- Add failing test cases https://github.com/typescript-eslint/typescript-eslint/issues/5752 — committed to sirreal/typescript-eslint by sirreal 3 years ago
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!
Why is that? I think the following should be allowed:
It’s identical to the function overloading case: either you keep
voidin return type, or you changevoidtoundefinedand manually returnundefinedin the end of the function.What about this case:
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
voidfor 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.
That is incorrect.
x != nulldoes not treat falsey values as false. It returns false if and only ifxisnull | undefined- i.e. it is equivalent tox !== null && x !== undefined.I would like to be clear here:
This issue has nothing at all to do with the general case of
| voidin return types. That is a topic for a separate issue. This issue is specifically for allowing| voidin the return type of a function that has an overloaded declaration that returns a barevoid.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.
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.
With
() => undefined | number, the first function does not typecheck (typevoidis not assignable to typeundefined | number).I’ve stumbled upon this issue working on a wrapper function around immer. The
recipeargument to theproducefunction 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 | stringreturn type would be a direct “no” from this rule… but it’s also the only way to support overloads. Accepting PRs for this case!