-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Allow non returning functions to have contextually required undefined
return type
#36288
Comments
I like this idea 👍 |
This would be great. I'm working with an API that's typed in closure compiler's ES4 type system as e.g.
What needs to happen to allow the listener callback type to omit return statements? Any chance of this landing in a future release? |
I have a lot of code that has functions with return types of I'd be happy to try to take this on myself, if a project member could give some pointers as to how such a thing could be implemented. The idea is that any argument accepting a function parameter whose return type includes |
Do note that an explicit return statement without any value causes TypeScript to allow the const ok = (): undefined => {
return; // This explicit return statement is currently required
}; |
Consider ReactJS's const f: () => void = () => 42;
useEffect(f); IMO, an API which expects a function that returns void means it will not look at the return value, which isn't the case. But if the API had been correctly defined as useEffect(() => {}); I believe accepting this suggestion would help defining better APIs. |
I'd like to vote against this -- or at least provide it as a tsconfig option (so that we're not forced to use it). In fact, I would like to propose making returns more explicit than they are today. Currently the compile error goes away providing your method has at least one terminal branch with an explicit return... it would seem safer IMO if the error only goes away once all terminal branches have an explicit return: I have many Consider bugs like this, which would be prevented if every terminal branch required a function foo(): number | undefined {
if (someFunction()) {
return undefined
}
const value: number = someOtherFunction()
anotherFunction(value)
// return value <- The code I forgot to write! Today this compiles fine... but it shouldn't IMO.
} So tbh rather than making the returns more implicit, it would be safer to make them more explicit. (@falsandtru @RyanCavanaugh sorry for the "thumbs downs": your comments are great, I just wanted the maintainers to see at a glance that someone has an issue with the proposed change, as currently it looks unanimously in favour 😄) |
@ljwagerfield it sounds like you should turn on the |
Excellent, so it does! In that case, assuming Thanks again @RyanCavanaugh ! |
Very cool I like it this would have saved me two hours of bugfixing earlier 👍 |
This would be a great feature to incorporate into TS! |
It sounds great. In this example,
It would be more consistent to allow This change seems to have no drawbacks. What blocks approval of it? |
I'm all for this! This would allow typing event handlers as Edit: The current implementation still doesn't allow this as it blocks unions with |
Thank you @saschanaz! |
Search Terms
#36239
Suggestion
Allow non returning functions to have contextually required
undefined
return type.Use Cases
Examples
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: