microsoft / microsoft/TypeScript

Feature Request: Make ES module exports conform to an interface using triple slash directive

Aperta
#32,423 4 commenti 18 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

Search Terms

ESM, ES module, EcmaScript Module, Interface, Exports, triple slash directive, module

Suggestion

I want to be able to enforce and ESM's exports to conform to an interface.

Here's an ESM that exports some values:

carbon (45)

This ESM is going to be consumed by some system; the ESM acts as configuration for that system.

The system could expose interfaces/types for all possible exports, and users apply them manually:

carbon (47)

But this is manual work, and thus error-prone.

I propose to be able to do:

carbon (48)

The exact syntax TBD.

Adding

/// <exports name="ConfigInterface" from="system-that-needs-configuration" />

will tell typescript about which interface the ESM exports should adhere to and where to find it.

If this is implemented in a triple slash directive this can also work for non-ts files.

Use Cases

I want users of storybook to be able to configure it with ease using modern code.
CommonJS isn't tree-shake-able, which is important to us. There's a pretty detailed RFC for this feature for storybook here: https://docs.google.com/document/d/15aAALZBl0GTBEKgJN219ebzJ8LUJf2TVJ3hQdkNdLvQ/edit#

Tools like babel, eslint & webpack currently are or can be configured using CommonJS modules; as the ecosystem for ESM is improving, being able to add an interface to an ESM becomes really useful.

Tools could start using ESM for configuration more, which has clear benefits over CommoNJS.

Examples

Here's a config file that is annotated with the triple slash directive:
carbon (49)

Here's the interface that's being referred to:
carbon (51)

This should warn the user that logLevel = 'any' is not a valid value.

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

  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

Non viene indicato alcun file sorgente né alcun punto di ingresso dei test. Inizia esaminando la triple-slash directive proposta e gli esempi di export/interface ESM; il lavoro sarebbe completato quando saranno disponibili una sintassi concordata e una validazione che segnali export non validi come logLevel = 'any'.

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

Valutazione

Stack tecnologico
javascript, typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.