microsoft / microsoft/TypeScript

typechecking of return types broken with `try/catch` when `strictNullChecks` and `noImplicitAny` are enabled

Open
#45,592 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

🔎 Search Terms

try/catch, exception handling, strict mode

🕗 Version & Regression Information

Broken in 4.3.5, still broken in nightly

⏯ Playground Link

Playground link with strict mode

Disable --noImplicitAny and --strict (but leave --strictNullChecks) and typechecker will correctly note that val may have type null

💻 Code
declare function bar(): any;

async function foo(): Promise<string> {
    let val = null;
    try {
        val = bar();
    } catch (e) {}
    return val;
}
🙁 Actual behavior

Compile with --strict and observe no errors
Compile simply with --strictNullChecks and typechecker will correctly note that val may have type null and does not conform to the return type

🙂 Expected behavior

typechecker should correctly determine that val can have type null and does not conform to the return type

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 linked TypeScript Playground reproduction, using the shown async function and comparing --strict with --strictNullChecks. Trace the typechecker's handling of the try/catch assignment and return type; done means the strict configuration reports that val may be null and cannot satisfy Promise.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.