microsoft / microsoft/TypeScript

Using declarations like a statement leads to strange bugs.

Open
#62,896 7 comments 0 reactions 0 assignees View on GitHub
Bug Domain: check: Control Flow Help Wanted
Dominant language
Go
Stars
111k
Forks
14.3k
PR merge metrics
PR metrics pending

Description

### 🔎 Search Terms

function declaration statement

### 🕗 Version & Regression Information

v 5.9.3

### ⏯ Playground Link

https://www.typescriptlang.org/play/?target=99#code/JYMwBAFALgTgrgUwJRgG4EMZgB5gLxgAMA3ALABQAxgPYB2AztQDYIB0T1A5hNksWAHoB1ANYUKIalggsoYYPiL8FAHjABmZQGotKDFgBWi4GSp1GLdlwgGUQhDBhT5tMDQC2AB2AsAXGCNgejA4egQAEzAAIwRJGARohGBaTjB0enpgTloAGjBaajAHJyxksHhaKGB3BLA68XJQSFhEFBA4WkoqujAQCBQAbwBfCX6hAqLHZzKPbxYwKpq8qLg5YunXCsWEfwBJV3pYYC6wd2pwhJoLvPbO7oY3dFc6JgBPRLALyiZMCLS5KDUTxgFioBBMMAbTIXNLRDiUERgIA

### 💻 Code

```ts
if (true) var x = 0;
console.log(x); //ok

for (let i = 0; i < 3; i++) var j = i;
console.log(j) //error in compile: j is used before being assign, no error in runtime

if (true) function f() {}
f()//no error in compile time, but error in runtime: In strict mode code, functions can only be declared at top level or inside a block
```

### 🙁 Actual behavior

-

### 🙂 Expected behavior

-

### Additional information about the issue

The questions above was raised only in academic purpose. I understand that there are no real use cases to apply code above.

Contributor guide

Open the contributing guide

Research direction

Start with the linked TypeScript Playground and reproduce the three declaration examples in the issue, comparing compiler diagnostics with runtime behavior. The issue does not specify an expected result, so establish whether the intended fix is a diagnostic, emitted-code change, or documented JavaScript semantics before identifying the relevant compiler area and tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.