microsoft / microsoft/TypeScript

Type definition for what `for await` takes

Aperta
#36,153 3 commenti 4 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

I haven't found this request on the current issues so I'm opening one.

Search Terms

"for await" asynchronous generators

Suggestion

If for await can take both an Iterable and an AsyncIterable then I'd like a type alias

type ForAwaitable<T> = Iterable<T> | AsyncIterable<T>;

// or maybe ForAwaitable<T, U = T> = Iterable<T> | AsyncIterable<U>;

Better names are welcome.

Use Cases

type ForAwaitable<T> = Iterable<T> | AsyncIterable<T>;

async function foo(chunks: ForAwaitable<Buffer>): Promise<void> {
  for await (const chunk of chunks) {
    // ...
  }
}

await foo(someReadableStream);
await foo(inMemoryBufferArray);

Looking at https://tc39.es/proposal-async-iteration/#sec-createasyncfromsynciterator and after a test, if an object has both fields Symbol.asyncIterator and Symbol.iterator then the former takes precedence, but that use case should rarely happen.

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

Inizia esaminando gli alias ForAwaitable proposti e il caso d’uso per accettare valori Iterable e AsyncIterable. Controlla la specifica collegata sull’iterazione asincrona, in particolare CreateAsyncFromSyncIterator e la precedenza tra Symbol.asyncIterator e Symbol.iterator. Il lavoro sarà considerato completato quando si sarà raggiunto un design condiviso degli alias di tipo che modelli accuratamente gli input di for-await senza modificare il JavaScript emesso.

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
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.