Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

121
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda