deno-docs icon indicating copy to clipboard operation
deno-docs copied to clipboard

`noImplicitReturns` seems to be enabled

Open EdJoPaTo opened this issue 11 months ago • 2 comments

According to the documentation noImplicitReturns is not enabled by default:

https://github.com/denoland/deno-docs/blob/fc68d8496f2c37d025fc39290e089e210f6d155e/runtime/manual/advanced/typescript/configuration.md?plain=1#L56

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

EdJoPaTo avatar Mar 16 '24 16:03 EdJoPaTo

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.

thisisjofrank avatar Mar 21 '24 02:03 thisisjofrank

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

dsherret avatar Mar 27 '24 17:03 dsherret