microsoft / microsoft/TypeScript

require() is generated in JS although there's no runtime code

オープン
#61,395 コメント 5 件 リアクション 0 件 担当者 1 名 GitHub で見る

@andrewbranch がすでに取り組んでいます。

2025年3月14日 から。

Needs Investigation
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

### 🔎 Search Terms

require generated const enum
namespace require generated
namespace require const enum
namespace require
"export import" require

### ⏯ Playground Link

Multiple files, so can't use playground

### 💻 Code

```ts
// file1.ts
export namespace MyOtherNamespace {
export const enum Yay {
OK,
KO
}
}
```
```ts
// file2.ts
import { MyOtherNamespace } from "./file1";
export namespace MyNamespace {
export import NS2 = MyOtherNamespace;
}
```
```ts
import { MyNamespace } from "./file2.ts";

const val = MyNamespace.NS2.Yay.KO
```
```js
// Compiled code
const file2_1 = require("./file2"); // notice the variable name which is just "filename_1", as it is not used anywhere
const val = 1;
```

### 🙁 Actual behavior

Compiling an unused `const file2_1 = require("./file2");` line

### 🙂 Expected behavior

Do not compile unused line.

### Additional information about the issue

The problem is that this file is a types file, so it is installed only in develpment environment (`npm install -D xxx`) so in runtime it is failing because it can't find the required module.

If I modify the `MyNamespace` namespace to directly export a namespace instead of doing an `export import` it works, but we have a lot of namespaces so we have them separated into files, and couldn't find another way to export an imported namespace inside another namespace.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。