Skip to content

Unreachable code is not detected for methods that return never #56049

Closed as not planned
@patrickkunka

Description

@patrickkunka

🔎 Search Terms

  • type narrowing
  • return type never
  • error
  • throw
  • method
  • chaining
  • error 7027

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for any entries relating to return type never

⏯ Playground Link

https://tsplay.dev/wQqAZw

💻 Code

const throwError = (message: string): never => {
  throw new Error(message);
}

type Thrower = typeof throwError;

const errorContext = {
  throwError
};

type ErrorContext = typeof errorContext;

const test1 = (thrower: Thrower) => {
  throw new Error('foo');

  // following code results in 7027 "unreachable code detected" as expected

  console.log('bar');
};

const test2 = (thrower: Thrower) => {
  thrower('foo');

  // following code results in 7027 "unreachable code detected" as expected

  console.log('bar');
};

const test3 = (errorContext: ErrorContext) => {
  errorContext.throwError('foo');

  // following code does NOT result in 7027, although the code is unreachable at runtime

  console.log('bar');
};

🙁 Actual behavior

If a function that returns never is a method, it does not result in error 7027 "unreachable code detected" or any
expected type-narrowing for subsequent code when called.

🙂 Expected behavior

Any code after a call to a function that always returns never should result in error 7027 "unreachable code detected" and any expected type-narrowing regardless of whether that function is method or a standalone function.

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions