microsoft / microsoft/TypeScript

Allow dynamic usage of `import = require` syntax

Offen
#60,884 5 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔍 Search Terms

dynamic import, require, verbatimmodulesyntax

✅ Viability Checklist
⭐ Suggestion

I propose that import = require() should be allowed anywhere a require() statement is allowed (under appropriate module settings).

function foo() {
   import foo = require('./module.js');
}
📃 Motivating Example

I've been trying to convert a commonjs project to verbatimModuleSyntax. Currently dynamic JSON imports are written as

function foo() {
   const json = await import('./foo.json');
}

which are rewritten as Promise.resolve().then(() => require('./foo.json')); by TS due to my module setting. The import() syntax currently has the advantage that TS statically analyzes and provides types for the imported JSON.

I'd like to convert the JSON import to a require() call, however, a bare require() loses static analysis. Therefore, I'd like to be able to use

function foo() {
   import json = require('./foo.json');
}

in order to have type inference and static analysis of the json file (also to have it copied to the output build).

(using a node import()-from-CJS is not available to me at this time)

💻 Use Cases
  1. What do you want to use this for? dynamic require()s with static analysis, under verbatimModuleSyntax
  2. What shortcomings exist with current approaches? No static analysis of dynamic require()s.
  3. What workarounds are you using in the meantime? Not using verbatimModuleSyntax

Somewhat relates to https://github.com/microsoft/TypeScript/issues/60598

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 der Prüfung des zugehörigen Issues #60598 und der aktuellen Behandlung von dynamischem import(), require() und verbatimModuleSyntax. Die Änderung ist abgeschlossen, wenn funktionslokales import = require() unter geeigneten Moduleinstellungen akzeptiert wird und dabei die statische Analyse sowie das Verhalten der JSON-Ausgabe erhalten bleiben.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
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.