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

fix: handle return statements in functions correctly #470

Merged
merged 5 commits into from
May 14, 2024

Conversation

lucacasonato
Copy link
Member

@lucacasonato lucacasonato commented May 13, 2024

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.

Fixes #468

Copy link
Contributor

@marvinhagemeister marvinhagemeister left a 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.

@lucacasonato
Copy link
Member Author

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 void and never based on type information.

For example this code block infers never as the return type:

() => {
  Deno.exit();
}

But this example infers void:

() => {
  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.
@lucacasonato lucacasonato force-pushed the function_return_void_inference branch from fe44272 to 32262a3 Compare May 14, 2024 08:59
Copy link
Contributor

@marvinhagemeister marvinhagemeister left a 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 👍

Copy link
Member

@dsherret dsherret left a 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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

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

Successfully merging this pull request may close these issues.

Update when we can infer void from a function body
3 participants