microsoft / microsoft/TypeScript
The module namespace object returned by __importStar is not the same when import the same module multiple times
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
### 🔎 Search Terms
- `__importStar`
- `module object`
- `namespace object`
### 🕗 Version & Regression Information
- Version: 6.0.2
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about "namespace import". I did not found any information related to this.
### ⏯ Playground Link
https://www.typescriptlang.org/play/?target=99&jsx=0&module=1&ts=6.0.2#code/DYUwLgBADgjBC8ECWBbKB7ATmAFAIgDMBnPASgG4BYAKFEigCYFk0tdCSKaaAFTdFEiIgAdAENgwHAG1YAGmgMAuqRFgAFiAB2OGcRgLiy0ggB8EAN40INiAGN0WoulAjg6AOY59CeIiMUEAD0QRBgmACuIDQAvlzUQA
### 💻 Code
```ts
let p1 = import("fs");
let p2 = import("fs");
Promise.all([p1, p2]).then(([fs1, fs2]) => {
console.log(fs1 === fs2); // expected true
});
```
### 🙁 Actual behavior
When the compiled code with node is run, `false` is printed. This means that the two imported module namespace object is different. each `__importStar` call create a new module namespace object.
### 🙂 Expected behavior
`true` should be printed. The module namespace objects of these two `import("fs")` should be the same object.
If we run this script directly by `node` without compiled, `true` will be printed.
### Additional information about the issue
To reproduce this issue, "module" in "tsconfig.json" need to be set to "commonjs". This makes `tsc` use `__importStar` and `require` to import the module.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
Start with the __importStar helper and the CommonJS module path described in the issue, using the linked Playground example and a Node.js run as the reproduction. Compare the two Promise results and trace how repeated import("fs") calls create namespace objects; done means the compiled CommonJS case matches direct Node.js behavior and returns the same object.
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- node.js, typescript
- 领域
- build-system, compilers
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 冷清
- 描述清晰度
- 基本清楚
- 新手友好度
- 52/100