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

Calling a function that returns never, not always triggers an unreachable code error #53510

Closed
gabrielricardourbina opened this issue Mar 26, 2023 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@gabrielricardourbina
Copy link

Bug Report

πŸ”Ž Search Terms

  • return never
  • unreachable code

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about return never & unreachable code

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

declare const blue : (...args: any) => never
declare const hof: (fn :(a: (...args: any) => never) => void ) => void

const withBlue = ()=> {
    blue("test")
    return "OK" // Unreachable code detected
}

const withPink = (pink : (...args: any) => never)=> {
    pink("test")
    return "OK" // Unreachable code detected
}

const withGreen = (unk: unknown) => {
    const green = unk as (...args: any) => never
    green("test")
    return "OK"// Unreachable code NOT detected
}

hof((self)=>{
    self("nahh")
    return "Ok" // Unreachable code NOT detected
})

πŸ™ Actual behavior

When the function that returns never is inferred, it does not detect unreachable code
When the function that returns has been type-asserted, it does not detect unreachable code

πŸ™‚ Expected behavior

Calling a function that returns never should render all code bellow unreachable

@MartinJohns
Copy link
Contributor

MartinJohns commented Mar 26, 2023

It's a known design limitation, see #32695.

A function call is analyzed as an assertion call or never-returning call when

  • the call occurs as a top-level expression statement, and
  • the call specifies a single identifier or a dotted sequence of identifiers for the function name, and
  • each identifier in the function name references an entity with an explicit type, and
  • the function name resolves to a function type with an asserts return type or an explicit never return type annotation.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 27, 2023
@gabrielricardourbina
Copy link
Author

I understand, thanks, should I close this ticket?

@MartinJohns
Copy link
Contributor

Feel free to. Either you do it, or typescript-bot will do it in a few days, as this issue has the duplicate label.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants