microsoft / microsoft/TypeScript

Namespaces: use before declaration is not reported

未关闭
#61,891 4 条评论 3 个 reaction 已指派 1 人 在 GitHub 查看

还没有人认领这个 Issue。

Bug Domain: check: Control Flow Help Wanted
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

🔎 Search Terms

Namespace, use before declaration, initialized variable

🕗 Version & Regression Information
  • This is the behavior in every version I tried.
⏯ Playground Link

https://www.typescriptlang.org/play/?target=99&jsx=0&module=1&ts=5.9.0-dev.20250618#code/KYDwDg9gTgLgBAOwIYFtgGcxIMbDgQQG8AoOOUSWRVDLXAgRjhLLIunmwgXXgDMEcALxwAFAEphAPjgAiXlACWCAOazScAL7EN7KsjSYcefACYWrPZ2684SKFCQBPYXADa+AMwA6AG5IAGwBXYABdDW1dcA5qQzoTTws2aKouHnh-YLwRfAZvAQkI4k0gA

💻 Code
export namespace A{
  export namespace A1 {
    export const fn = () => "string"
  }

  export namespace A2{
    // should report use before initialization here 
    export const array = [A3.value]
  }

  export namespace A3{
    export const value = A1.fn()
  }
}
🙁 Actual behavior

Hi, using typescript V5+, the code provided did not reports any errors, and doesn't work in runtime due to use before initialization.

🙂 Expected behavior

It is expected that TS2448 and TS2454 will be reported.(use before declaration)
These are reported if the namespace keyword is omitted in the code example and it should be the same when namespaces are used.
It seems files with namespaces are always transpiled with the same format using any TS configuration. (everything becomes iife)

Additional information about the issue

Optimistically additional support for namespace will be added, such as not transforming the entire file to iife when namespaces are used- which happen on any configuration.

And maybe even an option to make a code like I've sent work, by isolating these namespaces properly, and resort their order by their variable dependencies between themselves, and report when cycling variable dependency between namespaces exists.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

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