microsoft / microsoft/TypeScript

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

Aperta
#43,363 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dall'esempio CodeSandbox e dalla relativa riproduzione in not_working.js, concentrandoti su come module.exports = {} modifica il tipo inferito di module. Conferma il comportamento previsto di NodeModule e aggiungi un test di regressione che mostri che createLogger(module) rimane valido dopo l'assegnazione dell'esportazione.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.