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.