microsoft / microsoft/TypeScript

TypeScript doesn't error on late definition/initialization of variables from outside an async closure.

Open
#39,499 1 comment 0 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
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 4.0-beta and Nightly (as of writing)

Search Terms: async closure variable initialization

Code

async function doSomething(): Promise<void> {
    const x = setTimeout(() => {
        const a = someVar + 1;
    }, 5000)

    await doSomethingSlowly();

    const someVar = 5;
}

Expected behavior:
There should be an error on const a = someVar + 1; since someVar may not have been initialized.

Actual behavior:
No error/warning with any settings that I could find.

Playground Link: link

Related Issues:

Comments
I see folks hit this every so often and it feels like something the TypeScript compiler could help warn against, in a similar way that 2448 and 2454 error.

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 TypeScript Playground reproduction and compare its behavior with the related diagnostics 2448 and 2454. Determine whether the compiler can soundly report that someVar may be uninitialized across the async closure, then validate any diagnostic against the example and existing related cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.