microsoft / microsoft/TypeScript

Using declarations like a statement leads to strange bugs.

未关闭
#62,896 7 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
Bug Domain: check: Control Flow Help Wanted
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

### 🔎 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.

贡献指南

打开贡献指南

调研方向

从链接的 TypeScript Playground 开始,重现 issue 中的三个声明示例,并比较编译器诊断与运行时行为。该 issue 没有指定预期结果,因此在确定相关的编译器区域和测试之前,先确定预期的修复是诊断、生成代码的变更,还是有文档记录的 JavaScript 语义。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, typescript
领域
compilers
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。