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

120
Vistas
How to discard Promise type in TypeScript?

I am using TypeScript and I have the following promise chain:

function bar(): Promise<boolean> {
    globalVar = doSomething();
    return Promise.resolve(true);
}

return foo()
    .then(() => {
        return bar();
    }).then(() => {
        return bas();
    }

In most cases, I need the return value of bas, but in one case I don't need it. What is the best way to discard the boolean type? I tried a cast like as Promise<void> but it doesn't work as the types void and boolean don't intersect.

Here is a minimal MVCE:

let globalVar: string = 'hello';

function bar(): Promise<boolean> {
    globalVar = 'world';
    return Promise.resolve(true);
}
function xyz(): Promise<void> {
    return bar();
}

TS Playground

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

0

This is a longstanding bug in TypeScript; see microsoft/TypeScript#12871. It's listed as being on the "Backlog" so I wouldn't expect to see a fix anytime soon if I were you. The easiest way to address this is just to use a type assertion (what you're calling a "cast") to suppress the error:

function xyz(): Promise<void> {
    return bar() as Promise<any> as Promise<void>;
}

the error on bar() as Promise<void> is just because the compiler doesn't see them as sufficiently related to do an assertion; all you have to do is assert to some intermediate type seen as related to both of them.

This change has the advantage of leaving the emitted JavaScript alone.


Another fix is to use an empty then(), which doesn't change the returned promise (it'll still be a boolean at runtime) but the compiler sees it as Promise<void>:

function uvw(): Promise<void> {
    return bar().then()
}

Or you could explicitly make sure there's no return value at runtime too:

function rst(): Promise<void> {
    return bar().then(() => { })
}

But both of these are just runtime-visible workarounds for a TS compiler bug, so I don't know if I recommend them.

Playground link to code

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could just add a .then behind your bar() that returns nothing:

let globalVar: string = 'hello';

function bar(): Promise<boolean> {
    globalVar = 'world';
    return Promise.resolve(true);
}
function xyz(): Promise<void> {
    return bar().then(() => undefined);
    /*
       // other possible callbacks
       return bar().then(() => {});
       return bar().then(() => {
          return;
       });
    */
}

With await/async it would look better:

let globalVar: string = 'hello';

function bar(): Promise<boolean> {
    globalVar = 'world';
    return Promise.resolve(true);
}
async function xyz(): Promise<void> {
    await bar()
}
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