Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule proposal: no-void-return-type #7250

Closed
6 tasks done
remcohaszing opened this issue Jul 17, 2023 · 2 comments
Closed
6 tasks done

Rule proposal: no-void-return-type #7250

remcohaszing opened this issue Jul 17, 2023 · 2 comments
Labels
enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on

Comments

@remcohaszing
Copy link
Contributor

Before You File a Proposal Please Confirm You Have Done The Following...

My proposal is suitable for this project

  • My proposal specifically checks TypeScript syntax, or it proposes a check that requires type information to be accurate.
  • My proposal is not a "formatting rule"; meaning it does not just enforce how code is formatted (whitespace, brace placement, etc).
  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Description

The void type in TypeScript shouldn’t exist, because it represents something that doesn’t exist in JavaScript. microsoft/TypeScript#42709 explains it in more depth.

There is are the @typescript-eslint/no-invalid-void-type and @typescript-eslint/no-meaningless-void-operator rules. I would like to propose the @typescript-eslint/no-void-return-type rule to disallow returning void, Promise<void>, and PromiseLike<void>.

Fail Cases

// This function returns undefined
function a(): void {}

// This function returns undefined
function b() {}

// This probably types a function that returns undefined
declare function c(): void

// This function returns a promise that resolves to undefined
async function d(): Promise<void> {}

// This function returns a promise that resolves to undefined
declare function e(): Promise<void>

// This function returns a promise-like that resolves to undefined
declare function e(): PromiseLike<void>

// If void is used as anything but a return value, it’s invalid anyway
type G = string | void

Pass Cases

// This function returns undefined
function a(): undefined {}

// This function returns undefined
function b(): undefined {}

// This probably types a function that returns undefined
declare function c(): undefined

// This function returns a promise that resolves to undefined
async function d(): Promise<undefined> {}

// This function returns a promise that resolves to undefined
declare function e(): Promise<undefined>

// This function returns a promise-like that resolves to undefined
declare function e(): PromiseLike<undefined>

Additional Info

TypeScript 5.1 loosened the rules for void/undefined, making it viable to stop using void.

This rule could be auto-fixable by replacing a return type of void with undefined, or adding an explicit return type if none of the branches return an explicit value.

It would also just report any usage of void.

@remcohaszing remcohaszing added enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Jul 17, 2023
@bradzacher
Copy link
Member

I don't personally agree with this, but regardless we don't look to making this change to ban features without really good reasons or significant value we can add.

These sorts of rules that ban features are always quite opinionated and thus are very divisive - it's not something we really want to get into now-a-days.

https://typescript-eslint.io/linting/troubleshooting/#how-can-i-ban-specific-language-feature

example playground

@bradzacher bradzacher closed this as not planned Won't fix, can't repro, duplicate, stale Jul 17, 2023
@bradzacher bradzacher added wontfix This will not be worked on and removed triage Waiting for team members to take a look labels Jul 17, 2023
@remcohaszing
Copy link
Contributor Author

Thanks for the tip to use no-restricted-syntax. It doesn’t catch implicit void return types, but it’s sufficient for my use case.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants