Skip to content

undefined return type requires explicit return #40133

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

Closed
saschanaz opened this issue Aug 19, 2020 · 4 comments
Closed

undefined return type requires explicit return #40133

saschanaz opened this issue Aug 19, 2020 · 4 comments

Comments

@saschanaz
Copy link
Contributor

TypeScript Version: playground

Search Terms: undefined return type

Code

function test(): undefined {
  console.log("hey")
}

Expected behavior: This should be allowed.

Actual behavior: TS requires the return type to be void or any.

Playground Link: https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABFApgZygCgJQC5HgAmKwMYKhiA3gFCKIQJpwA2KAdC3AOaYBEACxQBPPthoBfIA

@sirmmo
Copy link

sirmmo commented Aug 19, 2020

void return type is not undefined. ok, javascript is so useless and badly thought that returning nothing and returning undefined are the same, but they are not at least not in typescript. undefined === void returns a syntax error. And that is because undefined is anyway something and not the absolute nothingness that is represented by void.

@annevk
Copy link

annevk commented Aug 19, 2020

I think this might be a duplicate of #36288.

Given the behavior of

function test(): void {
  return undefined;
}

I think @saschanaz has a point, FWIW.

@saschanaz
Copy link
Contributor Author

undefined === void returns a syntax error. And that is because undefined is anyway something and not the absolute nothingness that is represented by void.

AFAIK that's not because undefined is an actual value, it's because void is null | undefined (without --strictNullCheck) and thus it's inappropriate to blindly assign void to undefined.

@saschanaz
Copy link
Contributor Author

I agree that this is a duplicate, closing.

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

No branches or pull requests

3 participants