microsoft / microsoft/TypeScript
Allow dynamic usage of `import = require` syntax
Dieses Issue hat noch niemand übernommen.
- 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
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ 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
- What do you want to use this for? dynamic require()s with static analysis, under verbatimModuleSyntax
- What shortcomings exist with current approaches? No static analysis of dynamic
require()s. - What workarounds are you using in the meantime? Not using verbatimModuleSyntax
Somewhat relates to https://github.com/microsoft/TypeScript/issues/60598
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Ö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