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
[explicit-function-return-type] Allow implicit void functions #50
Comments
I think we should do it in different way, if there is no type specified, we should check if return type if consistent: it can be easily accomplished with code path |
There's four cases you have to test on each path:
All three are the same type-wise. The question is - what is the motive behind this rule?
function foo(param : string) { // return type = string
return 'string' + param;
}
foo('').length // runtime safe!
// modify code
// strict return type = string | null
// non-strict return type = string
function foo(param : string) {
if (!string) {
return null;
}
return 'string' + param;
}
foo('').length // non-strict, compile-time safe, but breaks at runtime repl - use the options to turn strict nulls on/off So the question is - why strictly enforce return type on
So the question is this - is the saving of 5-7 characters per void function and breaking of motivation (1) worth the code complexity that will be required to do function path and return type analysis? |
why did you close this one? this issue is not resolved yet :> |
I understood that you didn't want to implement it ... and I've personally gotten used to explicitly typing |
currently we will have to add a lot of logic for this but, when type analysis will be available in parser we will have access to it directly. I will like to implement this feature, but not before we are going to update parser to v21.* |
In that case, forgive me for closing. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Is there a separate issue for tracking this? I couldn't find anything. |
@davidje13 type information has been available to rules for quite a while. Looking at this enhancement again, I think that whilst it's easy to do, it goes against the intention of the rule. We don't want a special case for |
@bradzacher my main interest in this is for Jasmine/Jest testing. To give you an idea of what that looks like: describe('some thing', (): void => {
describe('some method', (): void => {
it('does stuff', (): void => {
});
it('does more stuff', (): void => {
});
it('does something asynchronously', async (): Promise<void> => {
await foobar();
});
});
}); Clearly this is very verbose (especially for the Currently I am setting |
An alternative fix which would work for my use-case is extra logic if a function is defined inline as a parameter to another (typed) function: if the return type could implicitly match the expected return type, and it can be verified that the implementation does indeed conform to it, omitting an explicit return type seems logical. Presumably the same logic could apply if a lambda is assigned to a typed variable. In the above case, because |
I direct you to either have a look at the rule's documentation, or read the collapsed comments where another user had the same problem you have. For tests specifically, you could use eslint overrides to disable the rule. Considering tests aren't production code, it isn't a problem to have more relaxed linting. |
@bradzacher in theory that seems to be what I'm looking for, though I'm observing the same behaviour as thisissami. Presumably it's a bug; if I can narrow it down I'll open a new issue. I'm already using eslint overrides for relaxing rules for tests, but I still have helpers, etc. which I'd like to have some certainty around. |
@bradzacher I have created #679 which demonstrates this not working in a minimal case. |
@davidje13 on my computer, all it took was a VScode reload for this to work properly. |
@thisissami that's weird considering the feature was apparently never implemented (see the PR for the issue I raised); are you sure you didn't set another option or disable the rule entirely somehow? |
@davidje13 i have the following in my "@typescript-eslint/explicit-function-return-type": [
"warn",
{
allowExpressions: true,
allowTypedFunctionExpressions: true,
}
], When I first chimed in this thread, I had first set the rules as per immediately above - and was getting warnings on all my However, doing some digging now - it's the |
Man I wish this feature existed. 90% of my functions are void - it would be very nice way of telling me "hey, this function actually returns something, is this the expected type?". But adding void, void, void everywhere makes it harder to read/write and adds no value. |
Can't believe that this is closed....
The way I see it, it's just a sensible default to save oneself a lot of time not an inconsistency. The way it is, I am so annoyed by having to write
is easier to distinguish then:
It wouldn't have to be a default, just an optional rule. |
We won't be building type information into the rule because:
If this is something that you few would find valuable, this code is released under an MIT licence, so you're welcome to create, say, |
It would be nice if this rule had an option to ignore errors for void functions not explicitly marked as such.
Currently:
After:
The text was updated successfully, but these errors were encountered: