Skip to content

False positive error when accessing property method that returns never using --strict #46254

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
olafurpg opened this issue Oct 7, 2021 · 4 comments

Comments

@olafurpg
Copy link

olafurpg commented Oct 7, 2021

Bug Report

I get a compile error when calling a property method that returns never using --strict, when I expect to get no compile error. I am unable to reproduce the error with toplevel functions.

🔎 Search Terms

  • never strict label:bug
  • never property
  • false positive never label:bug

🕗 Version & Regression Information

  • I am able to reproduce this behavior with versions 4.1.3 (my application), 4.4.3 (the minimized reproduction above) and 4.5.0-beta (playground link)
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about never

⏯ Playground Link

Playground link https://www.typescriptlang.org/play?ts=4.5.0-beta#code/EQVwzgpgBGAuBOBLAxrYBuAUAMxAO1UQHs8oAjIogWwAoBKALijwgDcJ4oBvTKKWABbwiAd2YQxAUXjD4NAOQVq8upgC+mTMhJwoAeRCwOUALzdefcpVqNx7Tj0tP+Q0eKkyicxdZUW+GhpaADYAhmBgUABCoQ7+UAAOIGTBKFbUAGL4hCT0THggVGTGjs58SjbxGk5JKWkVAArCCRywAJ55zIXFcWV8BkbwAHQV9FXqmEA

💻 Code

Minimized reproduction

 cat boom.ts
function boom(): never {
  throw new Error('boom')
}

const Outer = {
    boom(): never {
        throw new Error('boom')
    }
}

class Bar {
    public boomFunction(): number {
        boom()
    }
    public boomProperty(): number {
        Outer.boom()
    }
}

🙁 Actual behavior

❯ npx -p [email protected] tsc --strict boom.ts
boom.ts:18:28 - error TS2366: Function lacks ending return statement and return type does not include 'undefined'.

18     public boomProperty(): number {
                              ~~~~~~


Found 1 error.

🙂 Expected behavior

I expected there to be 0 errors because there is no error for boomFunction(), only boomProperty().

Workarounds

  • add return Outer.boom().
  • remove the --strict flag.
@olafurpg olafurpg changed the title False positive error when accessing property that returns never using --strict False positive error when accessing property method that returns never using --strict Oct 7, 2021
@MartinJohns
Copy link
Contributor

MartinJohns commented Oct 7, 2021

Duplicate of #12825.

Although I'm surprised only one of those two errors. This changed with TypeScript 3.7, so it's most likely related to the changes of #32695. I would guess the same rules for assertion-functions apply here.

The --strict flag is only loosely related. Without it you most likely have strictNullChecks and noImplicitReturns disabled. The former allows undefined to be a valid value for number, and the latter allows to omit a return statement (resulting in an implicit undefined return value). Having either of these two flags enabled will result in an error.

@olafurpg
Copy link
Author

olafurpg commented Oct 7, 2021

@MartinJohns Nice find! That issue is indeed related but I don't think it's a duplicate. I removed the if condition and I'm still able to reproduce this issue, indicating it's unrelated to control flow. I updated the reproduction above to remove the if condition.

@MartinJohns
Copy link
Contributor

It's still related to control flow. It doesn't consider the Outer.boom() method to be "terminating" (aka never returning). The initial example of #12825 already works, but there have been several comments regarding this behaviour in that issue.

@olafurpg
Copy link
Author

olafurpg commented Oct 7, 2021

@MartinJohns You're right, this issue is about control flow. This is indeed a duplicate. Thank you!

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

2 participants