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
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too