microsoft / microsoft/TypeScript
Suggestion: support Node `require` in ES Modules
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
Search Terms
node require commonjs webpack conditional dynamic import
Suggestion
Node style require's are often used inside ES Modules for conditional imports. For example:
const getSentry = () => {
if (__CLIENT__) {
return require('./client-sentry').default;
} else {
return require('./server-sentry').default;
}
};
export default getSentry();
In this example, require returns any as per the typings provide by @types/node.
In order to acquire types, we have to use import types:
type ClientSentry = typeof import('./client-sentry');
type ServerSentry = typeof import('./server-sentry');
const getSentry = () => {
if (__CLIENT__) {
return (require('./client-sentry') as ClientSentry).default;
} else {
return (require('./server-sentry') as ServerSentry).default;
}
};
export default getSentry();
I would like to suggest that TypeScript supports require calls inside of ES Modules (in Node envs). This way the workaround would not be necessary. Alternatively, perhaps TypeScript could make it possible for @types/node to improve the return type of require, e.g.
interface NodeRequireFunction {
<Id extends string>(id: Id): typeof import(Id);
}
Note that require is desirable over dynamic imports because it is synchronous.
Checklist
My suggestion meets these guidelines:
- 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, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia dagli esempi di require condizionale dell’issue e dalla dichiarazione NodeRequireFunction collegata in types/node/globals.d.ts. Non vengono indicati file del repository né test; il lavoro sarebbe completato quando le chiamate sincrone a require nei moduli ES saranno sottoposte al controllo dei tipi senza i cast mostrati, preservando il JavaScript emesso.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- node.js, typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 30/100