Quantcast
Channel: Active questions tagged visual-studio-code - Stack Overflow
Viewing all articles
Browse latest Browse all 97445

Typescript Promise not returning optional variable

$
0
0

I have a function basically like this

function foo(): Promise<MyClass | undefined> {
    if(somethingGoesWrong) {
        return Promise.resolve(undefined);
    }
    return Promise.resolve(new MyClass());
}

Outside the function, I call this

const x = await foo()

The problem is my IDE which is VSCode won't treat x as MyClass | undefined. It will just be MyClass.

I don't know is this an IDE problem or there are rules such as "Promise can't resolve undefined".

I know a better way to do this is probably throw some error instead of return undefined, I just want to know why is this not working or seems not working.


Viewing all articles
Browse latest Browse all 97445

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>