Skip to content

never type does not work as expected in the function of a class #54265

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
SkyGrasperKnight opened this issue May 16, 2023 · 4 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@SkyGrasperKnight
Copy link

Bug Report

πŸ”Ž Search Terms

  • never
  • class
  • function

πŸ•— Version & Regression Information

  • Version: 5.0.4
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about never type and class

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

function throwErrorFunction() : never {
  throw new Error("Error in function")
}

class ErrorService {
  throwErrorClass(): never {
    throw new Error("Error in class")
  }
}


function neverNotWorkingAsExpected() {
  const errorService = new ErrorService();
  errorService.throwErrorClass();
  
  // The following line should be detected as unreachable
  throwErrorFunction();
}

function neverWorkingAsExpected() {
  throwErrorFunction();

  // The following code is detected as unreachable
  const errorService = new ErrorService();
  errorService.throwErrorClass();
}

πŸ™ Actual behavior

Code following a function of a class of type 'never' is not considered inaccessible.
It does not have the same behavior as a function that is not in a class.

πŸ™‚ Expected behavior

The never type is correctly handled when used in a function of a class.
So after using the function, the following code is considered unreachable.

@MartinJohns
Copy link
Contributor

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.

Duplicate of #36753.

@fatcerberus
Copy link

In particular, the important part here is...

each identifier in the function name references an entity with an explicit type

"Function name" meaning "call target", in this case errorService.throwErrorClass. errorService doesn't have a type annotation.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 16, 2023
@microsoft-github-policy-service

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

@microsoft-github-policy-service

This issue has been marked as a 'Duplicate' 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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants