Skip to content

Error for explicit return type with no return statements is misleading #53559

Closed

Description

function foo(): string {
}

Currently in nightly, you'll get the error

error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.

But this error message is inaccurate, as you can now annotate with unknown.

Activity

  1. fatcerberus commented on Mar 28, 2023

    @fatcerberus

    In fact can't you now annotate with any type which is inhabited by undefined?

  2. DanielRosenwasser commented on Mar 28, 2023

    @DanielRosenwasser
    MemberAuthor

    Pretty much, which means that if you wrote

    function foo(): string | undefined {
        // oops, forgot to fill this in
    }

    that'll be okay in TypeScript 5.1. You'll need to enable --noImplicitReturns to catch something like that.

  3. RyanCavanaugh commented on Sep 19, 2026

    @RyanCavanaugh
    Member

    Fixed by PR #53607. In 5.1.0-dev.20230401, (): unknown => {} was accepted while TS2355 said only void and any could omit a return. Starting with 5.1.0-dev.20230402, both the string and unknown forms report TS2355, whose text correctly includes undefined, void, and any. The same result is present in TypeScript 6.0.3 and the current native compiler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptDomain: Error MessagesThe issue relates to error messagingFixedA PR has been merged for this issueRescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions