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 is ignored if strictNullChecks is true #51312

Closed
jrobber opened this issue Oct 25, 2022 · 4 comments
Closed

noImplicitReturns is ignored if strictNullChecks is true #51312

jrobber opened this issue Oct 25, 2022 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@jrobber
Copy link

jrobber commented Oct 25, 2022

Bug Report

If strictNullChecks is set to true directly or just by having the strict flag set to true
THEN noImplicitReturns does not work

🔎 Search Terms

noImplicitReturns
strictNullChecks
ignored
doesn't work
strict
strict mode

🕗 Version & Regression Information

Not sure, I just jumped on a new project, and they have this problem and it's conflicting with my project that uses it.

If possible, please try testing the nightly version of TS to see if it's already been fixed.
For npm: typescript@next

I tried this, and the issue is still present on: 4.9.0-dev.20221025

Please keep and fill in the line that best applies:
-->

  • This is a crash
  • This changed between versions ______ and _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about noImplicitReturns
  • I was unable to test this on prior versions because _______

⏯ Playground Link

To repro, just remove line 3: // @strictNullChecks: false

Workbench Repro

💻 Code

// @noImplicitReturns: true
// @strict: true
// @strictNullChecks: false


function doThing(name: string) {
    if (!name) {
        return;
    }
    return name;
}

Workbench Repro

🙁 Actual behavior

With strictNullChecks set to true, the error for noImplicitReturn is not visible.

🙂 Expected behavior

With strictNullChecks set to true, the error for noImplicitReturn should still be not visible.

@RyanCavanaugh
Copy link
Member

This is the intended behavior.

Under strictNullChecks: true, there's no real danger with this code - there's an explicit return statement, which is 100.0% equivalent to return undefined, so the inferred return type is string | undefined and any caller can see what they're about to get.

Under strictNullChecks: false, this code is pretty dangerous, since the inferred return type is string even though one of the code paths clearly doesn't actually return a value.

noImplicitReturns still warns you about codepaths which don't hit an explicit return statement:

function doThing(name: string) {
    if (!name) {
        
    } else {
        return name;
    }
}

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Oct 25, 2022
@jrobber
Copy link
Author

jrobber commented Oct 25, 2022

@RyanCavanaugh Thanks for the explanation. Was it in the docs or in the FAQ somewhere that I missed?

@RyanCavanaugh
Copy link
Member

I don't think the docs go into that level of detail

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants