microsoft / microsoft/TypeScript

Wildcard/star export from type expression

Aperta
#59,758 0 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.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔍 Search Terms
  • "export ="
  • "export type ="
  • flatten type export
  • export top-level type
  • wildcard export type
  • star export type
  • "export *"
  • "export type *"
✅ Viability Checklist
⭐ Suggestion

Introduce syntax for exporting a "spread" type:

// 1. This is based on ES export lists and spread tuple types. It would only be
//    valid for types, since modules have to be statically analyzable:
export { ...ComplexType };
export type { ...ComplexType };

// 2. Not currently valid in normal type expressions, so might be
//    unexpected, even if it mirrors valid ES syntax. However, it might be
//    something to consider allowing independently, since it's not quite the
//    same as an intersection type, and would be useful for "overwriting"
//    properties:
type Spread = { ...ComplexType };
//              ^ Member 'Complex' implicitly has an 'any' type. (7008)

// 3. These are probably too "generic" and easy to confuse with other export
//    syntax, and doesn't make the required "object" shape obvious:
export ComplexType;          // spread
export type ComplexType;     // spread
export type Other = boolean; // NOT spread

// 4. Barrel export syntax would be a bit more explicit, and mirrors their
//    respective import syntax nicely:
import type * as ComplexType from "./a.ts";
export type * from ComplexType;       // inverse of the import
export type { Foo } from ComplexType; // also potentially nice to have

// 5. An alternative based on CommonJS top-level export syntax, but probably
//    undesirable to use for ES modules:
export = { Foo: "a", Bar: 1 }; // top-level value
export type = ComplexType;     // top-level ("spread") type

I'd personally prefer (4), but if extending spreading to object types is feasible, (1) would also be a natural choice.

📃 Motivating Example

Not having "spreadable" type exports requires a lot of repetition in cases like this one, which this feature would alleviate significantly:

export type ComplexType = {
   Foo: string,
   Bar: number,
};

// A. This is how it works right now. Every type name has to be repeated three
//    times each:
export type Foo = ComplexType["Foo"];
export type Bar = ComplexType["Bar"];
export { Foo, Bar };

// B. This is what you could do instead:
export type * from ComplexType; // (4)
export type { ...ComplexType }; // (1)

// C. You could even trivially allow mapped/filtered exports
export type * from {  [K in keyof ComplexType]: K extends 'Bar' ? never : ComplexType[K]; }; // (4)
export type { [K in keyof ComplexType]: K extends 'Bar' ? never : ComplexType[K]; };         // (1)
💻 Use Cases

See Motivating Example

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, test o punto di ingresso. Inizia esaminando le alternative di sintassi proposte e individuando i percorsi rilevanti di export e controllo dei tipi di TypeScript; il lavoro sarebbe completato quando fossero definiti il design, l'implementazione e la copertura delle regressioni, ma l'issue non sceglie quale alternativa implementare.

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

Valutazione

Stack tecnologico
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
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.