I wrote this function but it throws an error and I don't understand why...
const createObject = async (): Promise<MyType> => {
await Promise.resolve();
return {
async foo() {
await this.bar();
// do something else
},
async bar() {
await Promise.resolve();
// do something here
}
};
};
interface MyType {
foo: () => Promise<void>;
bar: () => Promise<void>;
}
Why does the function call in line 5 of this.bar() show an error? It tells me that this is either of type MyType or a PromiseLike<MyType>. The second possible type is the problem but I don't see in which case this might be a PromiseLike? Can't it assume that if that function is called, this is always an "loaded" object?
What is the most elegant way around this? Currently I am doing this (which is absolutely ugly and not understandable as the reader of the code):
await (await this).bar();
I also found out that setting the return type of the function createObject from Promise<MyType> to Promise<any> solves the problem but that isn't very accurate.