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

noImplicitReturns seems to be enabled #369

Open
EdJoPaTo opened this issue Mar 16, 2024 · 2 comments
Open

noImplicitReturns seems to be enabled #369

EdJoPaTo opened this issue Mar 16, 2024 · 2 comments

Comments

@EdJoPaTo
Copy link

According to the documentation noImplicitReturns is not enabled by default:

| `noImplicitReturns` | `false` | |

Taking the TypeScript example code for this option it errors.

export function lookupHeadphonesManufacturer(color: "blue" | "black"): string {
  if (color === "blue") {
    return "beats";
  } else {
    "bose";
  }
}
$ deno check example.ts
Check file:///tmp/example.ts
error: TS2366 [ERROR]: Function lacks ending return statement and return type does not include 'undefined'.
export function lookupHeadphonesManufacturer(color: "blue" | "black"): string {
                                                                       ~~~~~~
    at file:///tmp/example.ts:1:72

This is exactly the message documented for this TypeScript option. I think the documentation is wrong here, and this option is enabled by default?

Interestingly when setting it to false via deno.json the error still comes up via deno check.
I want this check, so it's not a problem for me. I just want to have the documentation being correct.

$ deno --version
deno 1.41.3 (release, x86_64-unknown-linux-gnu)
v8 12.3.219.9
typescript 5.3.3
@thisisjofrank
Copy link
Collaborator

This is a great catch, however it looks like this is actually a bug in typescript.

Check out these tests in TypeScript playground:

With noImplicitReturns set to false: https://www.typescriptlang.org/play?strict=false&noImplicitReturns=false#code/GYVwdgxgLglg9mABAGznA1iADgCQKYCGAJlgBYJ4DOAsgWCMAdCAE54sAUEcqLAXIgBEAI2Qg8gxAB8hopukEBKAZSgsYYAOaIA3gChEiGMERcecFogC8N2WImLdBw4jZRWSEYSiVBAbmcAX0Q8ZEo8JxdZOHD-IL1AoA
withNoImplicitReturns set to true: https://www.typescriptlang.org/play?strict=false#code/GYVwdgxgLglg9mABAGznA1iADgCQKYCGAJlgBYJ4DOAsgWCMAdCAE54sAUEcqLAXIgBEAI2Qg8gxAB8hopukEBKAZSgsYYAOaIA3gChEiGMERcecFogC8N2WImLdBw4jZRWSEYSiVBAbmcAX0Q8ZEo8JxdZOHD-IL1AoA

Both give the same error.

The docs are correct, in that noImplicitReturns is set to false by default.

@dsherret
Copy link
Member

dsherret commented Mar 27, 2024

It's not a bug in TypeScript. The error is because the function also returns undefined, but the return type declaration doesn't reflect that:

export function lookupHeadphonesManufacturer(color: "blue" | "black"): string | undefined { // need to update to this
  if (color === "blue") {
    return "beats";
  } else {
    "bose";
    // because this function implicitly returns undefined here
  }
}

Once that's done then there won't be an error, but with noImplicitReturns enabled the following will occur:

> deno check main.ts
Check file:///V:/scratch/main.ts
error: TS7030 [ERROR]: Not all code paths return a value.
export function lookupHeadphonesManufacturer(color: "blue" | "black"): string | undefined {
                                                                       ~~~~~~~~~~~~~~~~~~
    at file:///V:/scratch/main.ts:1:72

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