-
Notifications
You must be signed in to change notification settings - Fork 45
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
fix: handle return statements in functions correctly #470
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM code wise! I'm admittedly a bit unsure of the implications from product a product perspective. We seem to be able to very accurately infer whether a function should return void
to the point to where we could add the missing return type ourselves, rather than asking the user to do that.
In all the cases we are 100% sure, we do this inference already. However in the cases that we now disallow, TypeScript can infer both to For example this code block infers never as the return type: () => {
Deno.exit();
} But this example infers () => {
fetch()
} We cannot make this distinction without type info, so we'll require the user to specify a return type explicitly. This applies only to function bodies of function expression-like things, like function expressions, arrow functions, and function shorthand in object literals. I have included many other examples in the linked issue. |
We now correctly make a distinction between expression like and declaration like functions when inferring return types based on return statements. The biggest change here is that expression like functions that have no `return` statements in the function body will now not infer to `void` anymore. This is because in some cases TypeScript will instead infer the return type to `never`. This applies to function expressions, arrow functions, and object literal function expression shorthand. It does not apply to function declarations and class members.
fe44272
to
32262a3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, thanks for the further explanation 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, with some questions
Cow::Borrowed("add an explicit return type of 'void' or 'never' to the function") | ||
} else if *is_async { | ||
Cow::Borrowed("add an explicit return type of 'Promise<void>' or 'Promise<never>' to the function") | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice
We now correctly make a distinction between expression like and declaration like functions when inferring return types based on return statements.
The biggest change here is that expression-like functions that have no
return
statements in the function body will now not infer tovoid
anymore. This is because in some cases TypeScript will instead infer the return type tonever
. This applies to function expressions, arrow functions, and object literal function expression shorthand. It does not apply to function declarations and class members.Fixes #468