Skip to content

Union returntype with void must return a value #6326

Closed
@Pajn

Description

@Pajn

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.

Activity

RyanCavanaugh

RyanCavanaugh commented on Jan 4, 2016

@RyanCavanaugh
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

DanielRosenwasser commented on Jan 4, 2016

@DanielRosenwasser
Member

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

RyanCavanaugh

RyanCavanaugh commented on Jan 4, 2016

@RyanCavanaugh
Member

Not sure I understand the comment

DanielRosenwasser

DanielRosenwasser commented on Jan 4, 2016

@DanielRosenwasser
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

RyanCavanaugh commented on Jan 4, 2016

@RyanCavanaugh
Member

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

Pajn

Pajn commented on Jan 4, 2016

@Pajn
Author

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.

added this to the Community milestone on Jan 5, 2016
RyanCavanaugh

RyanCavanaugh commented on Jan 5, 2016

@RyanCavanaugh
Member

Approved, accepting PRs. Should be easy.

masaeedu

masaeedu commented on Jan 15, 2016

@masaeedu
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

RyanCavanaugh commented on Jan 15, 2016

@RyanCavanaugh
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.

modified the milestones: TypeScript 1.8, Community on Jan 25, 2016
added
FixedA PR has been merged for this issue
on Jan 25, 2016
added
SpecIssues related to the TypeScript language specification
on Mar 9, 2016
locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issueGood First IssueWell scoped, documented and has the green lightHelp WantedYou can do thisSpecIssues related to the TypeScript language specificationSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @Pajn@DanielRosenwasser@masaeedu@RyanCavanaugh@mhegazy

        Issue actions

          Union returntype with void must return a value · Issue #6326 · microsoft/TypeScript