microsoft / microsoft/TypeScript

Use any instead of unknown for AsyncGenerator optional .next parameter

Ouverte
#33,458 1 commentaire 4 réactions 1 personne assignée Voir sur GitHub

@rbuckton y travaille déjà.

Depuis le 17/9/2019.

Needs Investigation
Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

TypeScript Version: 3.6.2

Search Terms:
async generator, AsyncGenerator, optional .next

Code

The following causes a type error:

async function* sequence(iterable: AsyncIterable<number>): AsyncGenerator<number> {
  yield 12;
  try {
    // Type error: Cannot delegate iteration to value because the 'next' method of its iterator expects type 'undefined', but the containing generator will always send 'unknown'.
    yield* iterable;
  } finally {
    console.log('Cleanup!');
  }
}

Expected behavior:

I would've expected it to be a non-type error.

Problem:

The primary annoyance with this is that I'd like to use AsyncGenerator<T> just for specifying that .return() can be used without non-null assertions (.return!()) for early cleanup but AsyncGenerator<T> results in AsyncGenerator<T, any, unknown> so AsyncIterable<T> can't be delegated to.

Proposed solution:

Change interface AsyncGenerator<T = unknown, TReturn = any, TNext = unknown> to interface AsyncGenerator<T = unknown, TReturn = any, TNext = any.

This won't break anything as any is assignable to anything, and I doubt it'll be problematic as those using AsyncGenerator<T> (or AsyncGenerator<T, S>) presumably do not care about the .next parameter.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.