Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

272
Vistas
Properly typing a generator function that calls another generator function in TypeScript

I have a utility function which is a generator, like this:

function* myGenerator(): Generator<Foo, void> {
  ... // yield Foos
}

Now I want to wrap this utility function in a class, like this:

class MyWrapper {
  *generator(): Generator<Foo, void> {
    // call utils.myGenerator();
  }
}

The problem is: If I type MyWrapper.generator() as a generator with the * notation, I cannot simply implement it as return utils.myGenerator(), because a generator function returns void, and I will get a Type Generator<Foo, void> is not assignable to void error.

I can remove the * from MyWrapper.generator(), have the return type be Generator<Foo, void>, and return utils.myGenerator():

class MyWrapper {
  generator(): Generator<Foo, void> {
    return utils.myGenerator();
  }
}

... but losing * makes it less clear for the caller that this is a generator function (granted, the return type should give it away, but still).

I can also iterate through the "inner" generator and yield everything from it:

class MyWrapper {
  *generator(): Generator<Foo, void> {
    for (const x of utils.myGenerator()) {
      yield x;
    }
  }
}

... but this extra yielding layer seems silly.

Is there an obvious way I'm missing to have a generator function wrap another generator function and still maintain the * notation, such as a "yield everything from" syntax?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use yield* to yield all elements of another generator

type Foo = string
function* myGenerator(): Generator<Foo, void> {
    yield "A";
    yield "B";
}


class MyWrapper {
  *generator(): Generator<Foo, void> {
    yield* myGenerator();
  }
}

console.log([...new MyWrapper().generator()]) // ["A", "B"] 

Playground Link

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda