microsoft / microsoft/TypeScript

TypeScript does not recognize unreachable code after a method returning never is called outside of its defining scope

Open
#59,138 4 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Does this issue occur when all extensions are disabled?: Yes

  • VS Code Version: 1.89.1
  • OS Version: All

To Reproduce:

class SimpleThrower {
    public throwError(): never {
        throw new Error('');
    }

    public doSomething(): string {
        this.throwError(); // Directly call the never-returning function
        
        return 'Success'; // TypeScript recognizes that this line is unreachable
    }
}

() => {
    const example = new SimpleThrower();
    console.log(example.doSomething()); // This will throw an error
    example.throwError(); // Throws an error and stops execution
    console.log('foobar'); // TypeScript does not recognize that this line is unreachable
}

Playground Link

Expected behavior:

TypeScript should recognize that the line console.log('foobar'); is unreachable after example.throwError(); is called, similar to how it recognizes the unreachable code within the doSomething method.

Actual behavior:

TypeScript does not mark the line console.log('foobar'); as unreachable after the example.throwError(); call in the global scope or within a different function scope.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided Playground Link and compare how unreachable code is recognized inside doSomething after this.throwError() with the behavior after example.throwError() in the outer function. Trace the TypeScript control-flow analysis entry point responsible for calls to methods returning never; done means console.log('foobar') is marked unreachable in the latter case without regressing the existing method behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.