microsoft / microsoft/TypeScript

In a JS file, using module.exports in a file changes the type of module from NodeModule to typeof import("...")

オープン
#43,363 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

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

説明

Bug Report

When using a JS file with @types/node, the type of module changes from NodeModule to something strange (typeof import("/sandbox/not_working");) after including a line like module.exports = {}.

🔎 Search Terms

module.exports, NodeModule, typeof import

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about 4.2.3
⏯ Playground Link

CodeSandbox link with relevant code

💻 Code
/// <reference types="node"/>
// INFO: This is JS

/**
 * @param {NodeModule} aModule
 */
function createLogger(aModule) {}

createLogger(module); // INFO: module here is triggering the error.

// INFO: THIS LINE BREAKS IT
module.exports = {};

🙁 Actual behavior
var module: {
    "\"/sandbox/not_working\"": typeof import("/sandbox/not_working");
}

Argument of type '{ "\"/sandbox/not_working\"": typeof import("/sandbox/not_working"); }'
is not assignable to parameter of type 'NodeModule'.

  Type '{ "\"/sandbox/not_working\"": typeof import("/sandbox/not_working"); }'
  is missing the following properties from type 'NodeModule': require, id, filename, loaded, and 3 more.

ts(2345)
🙂 Expected behavior

I expected module to always be of type NodeModule.

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

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

はじめの一歩

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

調査の方向性

CodeSandbox の例とその not_working.js による再現から始め、module.exports = {} によって module の推論される型がどのように変わるかに注目してください。期待される NodeModule の動作を確認し、エクスポート代入後も createLogger(module) が有効であり続けることを示す回帰テストを追加してください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

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

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