HelloWorld
HelloWorld

Reputation: 1863

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

Upvotes: 2

Views: 1275

Answers (2)

t.niese
t.niese

Reputation: 40842

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()
}

Upvotes: 2

jcalz
jcalz

Reputation: 328302

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

Upvotes: 4

Related Questions