Open EdJoPaTo opened 6 months ago
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.
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
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.
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 viadeno check
. I want this check, so it's not a problem for me. I just want to have the documentation being correct.