microsoft / microsoft/TypeScript
TypeScript does not recognize unreachable code after a method returning never is called outside of its defining scope
Nobody has claimed this yet.
- 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
}
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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