microsoft / microsoft/TypeScript

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

Offen
#43,363 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem CodeSandbox-Beispiel und seiner not_working.js-Reproduktion und konzentriere dich darauf, wie module.exports = {} den abgeleiteten Typ von module ändert. Bestätige das erwartete NodeModule-Verhalten und füge einen Regressionstest hinzu, der zeigt, dass createLogger(module) nach der Exportzuweisung weiterhin gültig bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.