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 change proposal: no-useless-undefined add option to skip checking function body #2295

Open
Nkmol opened this issue Feb 28, 2024 · 0 comments

Comments

@Nkmol
Copy link

Nkmol commented Feb 28, 2024

It is quite normal for a function to return T | undefined, for example with the Map.get() function. For this function it is also common to do some input checks, or return value checks, before doing something with the value. For these cases it also common to do an early exit with return, this is however conflicting with eslint/consistent-return.

Currently, the rule of no-useless-undefined would error in the following case:

declare const map: Map<number, string>;
function valueOrUndefined(value: number) {
  return map.get(0);
}

declare const input: number | undefined;
function readInput() {
  if(input === undefined) {
    console.error("Pick a valid input value.")
    return undefined;
  }

  return valueOrUndefined(input); // returns string | undefined
}

I believe it is good to let the user pick its preference in this case. Personally, I like return undefined in his case.


Related to #1199
Related PR: https://github.com/sindresorhus/eslint-plugin-unicorn/pull/2232/files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant