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

Suggestion: disallow implicit return of undefined #2316

Closed
neilgalarneau opened this issue Mar 12, 2015 · 4 comments
Closed

Suggestion: disallow implicit return of undefined #2316

neilgalarneau opened this issue Mar 12, 2015 · 4 comments
Labels
Duplicate An existing issue was already created Suggestion An idea for TypeScript

Comments

@neilgalarneau
Copy link

It would be useful to be able to disallow an Explicitly Typed function/method from implicitly returning the value undefined.

One example function that would be detected and disallowed was given by @rolyp in #2176. In that example, running off the end of the function causes undefined to be returned implicitly.

Another example that should be caught is:

function u(): number {
  return;
}

A flag for enabling this option could be named --noImplictReturnUndefined.

This suggestion fits in with the meta-bug in #274 and maybe #2176.

@danquirk
Copy link
Member

Definitely fits well within either of those two larger options.

@danquirk danquirk added the Suggestion An idea for TypeScript label Mar 12, 2015
@RyanCavanaugh
Copy link
Member

We should use this as an opportunity to look at code patterns in the wild -- how many JavaScript|TypeScript functions "intentionally" use an expressioned return and unexpressioned return in the same function body, especially in those with return type annotations.

@neilgalarneau
Copy link
Author

I understand that TypeScript's goal is to be a super set of JavaScript, so legal JavaScript is legal TypeScript.

However, if I add types to my code, it is no longer JavaScript, and I would like TypeScript to help me fulfill the contract that the type usage represents.

If I declare a return type on my function and that function ends with "return;", I would like TypeScript to see that as an inconsistency & point that out to me.

@mhegazy
Copy link
Contributor

mhegazy commented Dec 9, 2015

--noImplicitReturns will flag missing a return statement. the main issue today is that undefined is a valid value for any type, and return; is equivalent to return undefined; i would say this falls under the bucket of #185

@mhegazy mhegazy closed this as completed Dec 9, 2015
@mhegazy mhegazy added the Duplicate An existing issue was already created label Dec 9, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants