Skip to content

TS2366: Function lacks ending return statement in function with unknown return type with implicit return on some code paths, despite noImplicitReturns turned off #53473

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
AlCalzone opened this issue Mar 23, 2023 · 5 comments Β· Fixed by #53490 or #53607
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Good First Issue Well scoped, documented and has the green light Help Wanted You can do this

Comments

@AlCalzone
Copy link
Contributor

AlCalzone commented Mar 23, 2023

Bug Report

πŸ”Ž Search Terms

lacks ending

πŸ•— Version & Regression Information

  • This changed between versions 5.0.2 and 5.1.0-dev.20230323

I believe this caused it.

⏯ Playground Link

https://www.typescriptlang.org/play?noImplicitReturns=false&ts=5.1.0-dev.20230323#code/GYVwdgxgLglg9mABFApgZygCgJQC5HgDWYcA7kgN4CwAUAJAzCKYCyAhlABYB0ATm2AAmcALY5EAPkQAGbgFZsiXiigheSKLxAoA3LQC+QA

πŸ’» Code

function test(): unknown {
	if (Math.random() > 0.5) return true;
}

πŸ™ Actual behavior

> tsc --noEmit

index.ts:1:18 - error TS2366: Function lacks ending return statement and return type does not include 'undefined'.

1 function test(): unknown {
                   ~~~~~~~

πŸ™‚ Expected behavior

No error, if the function return type is unknown I'd expect that to allow implicit return of undefined.

@fatcerberus
Copy link

fatcerberus commented Mar 23, 2023

This changed between versions 5.0.2 and 5.1.0-dev.20230323

The error message changed, but this is an error in 5.0.2 and earlier too:

image

@AlCalzone
Copy link
Contributor Author

AlCalzone commented Mar 23, 2023

Uh, the playground link had noImplicitReturns turned on. If you turn it off, it only becomes an error after 5.0.2. I'll update the link

@AlCalzone AlCalzone changed the title TS2366: Function lacks ending return statement in function with unknown return type with implicit return on some code paths TS2366: Function lacks ending return statement in function with unknown return type with implicit return on some code paths, despite noImplicitReturns turned off Mar 23, 2023
@fatcerberus
Copy link

fatcerberus commented Mar 23, 2023

What do you know, this actually broke some real-world code already. πŸ˜„
#53474 (comment)

@AlCalzone
Copy link
Contributor Author

Believe it or not, I found this in real-world code :D

@DanielRosenwasser DanielRosenwasser added Bug A bug in TypeScript Help Wanted You can do this Good First Issue Well scoped, documented and has the green light labels Mar 23, 2023
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 5.1.0 milestone Mar 23, 2023
@DanielRosenwasser
Copy link
Member

I think the simplest fix would just be to check for TypeFlags.Unknown - alternatively, we restore the isTypeAssignableTo(undefinedType, type) check.

Let's make sure that we test strictNullChecks on/off with other top-y types.

// @strictNullChecks: true,false

function foo(): unknown {
    if (Math.random()) return true;

    // implicit return
}

function bar(): {} {
    if (Math.random()) return true;

    // implicit return
}

function baz(): Record<string, any> {
    // implicit return
}

function blah(): any {
    // implicit return
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Good First Issue Well scoped, documented and has the green light Help Wanted You can do this
Projects
None yet
5 participants