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

Union returntype with void must return a value #6326

Closed
Pajn opened this issue Jan 2, 2016 · 9 comments
Closed

Union returntype with void must return a value #6326

Pajn opened this issue Jan 2, 2016 · 9 comments
Labels
Fixed A PR has been merged for this issue Good First Issue Well scoped, documented and has the green light Help Wanted You can do this Spec Issues related to the TypeScript language specification Suggestion An idea for TypeScript

Comments

@Pajn
Copy link

Pajn commented Jan 2, 2016

I have a method declared as stop(): void|Promise<void> {} which get the error

A function whose declared type is neither 'void' nor 'any' must return a value

As void is a valid return type I would expect that to be fully valid code. Specifying only void is working.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Jan 4, 2016
@RyanCavanaugh
Copy link
Member

Proposing changing spec section 6.3

An explicitly typed function whose return type isn't the Void or the Any type must have at least one return statement somewhere in its body

to

An explicitly typed function whose return type isn't the Void type, the Any type, or a union type containing the Void or Any type as a constituent must have at least one return statement somewhere in its body

@DanielRosenwasser
Copy link
Member

That still doesn't actually cover Promise<void> on its own though.

@RyanCavanaugh
Copy link
Member

Not sure I understand the comment

@DanielRosenwasser
Copy link
Member

The change you're proposing doesn't permit an async function whose return type annotation is Promise<void> to have no return statements.

@RyanCavanaugh
Copy link
Member

I don't think anyone would want that behavior (@Pajn, care to weigh in?)

@Pajn
Copy link
Author

Pajn commented Jan 4, 2016

It would be nice if Promise didn't require a return statement in an async function. However for the issue I had the already proposed change would be enough.

@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this and removed In Discussion Not yet reached consensus labels Jan 5, 2016
@RyanCavanaugh RyanCavanaugh added this to the Community milestone Jan 5, 2016
@RyanCavanaugh RyanCavanaugh added the Good First Issue Well scoped, documented and has the green light label Jan 5, 2016
@RyanCavanaugh
Copy link
Member

Approved, accepting PRs. Should be easy.

@masaeedu
Copy link
Contributor

@RyanCavanaugh Regarding:

An explicitly typed function whose return type isn't the Void type, the Any type, or a union type containing the Void or Any type as a constituent must have at least one return statement somewhere in its body

Isn't the emphasized part redundant? The following already compiles:

function f(): number | any {
}

I think the union resolution already recognizes that all types are subtypes of any, so any union involving any is just any.

@RyanCavanaugh
Copy link
Member

I think that's an implementation side effect of the compiler. The spec doesn't specify that number | any is equivalent to the Any type, though.

@mhegazy mhegazy modified the milestones: TypeScript 1.8, Community Jan 25, 2016
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jan 25, 2016
@DanielRosenwasser DanielRosenwasser added the Spec Issues related to the TypeScript language specification label Mar 9, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue Good First Issue Well scoped, documented and has the green light Help Wanted You can do this Spec Issues related to the TypeScript language specification Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants