้•œๅƒ็ซ™็‚น ยท ๆœฌ้กต็”ฑ็ฌฌไธ‰ๆ–น GitHub ๅช่ฏป้•œๅƒๆไพ›๏ผŒ้ž GitHub ๅฎ˜ๆ–น็ซ™็‚น๏ผŒไธๆŽฅๅ—ไปปไฝ•็™ปๅฝ•ๆˆ–ๅ‡ญๆฎ่พ“ๅ…ฅใ€‚ๅ‰ๅพ€ github.com
Skip to content

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

Description

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.

Activity

  1. MartinJohns commented on May 16, 2023

    @MartinJohns
    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.

  2. fatcerberus commented on May 16, 2023

    @fatcerberus

    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.

  3. microsoft-github-policy-service commented on May 19, 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.

  4. microsoft-github-policy-service commented on May 19, 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.

  5. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions