type MyReturnType = Promise<boolean> | void
interface TestInterface {
testFunction(): MyReturnType;
}
class TestClass implements TestInterface {
testFunction() {
}
}
const testObject = new TestClass();
testObject.testFunction()
.then(() => { });
Why does it through the following error?
Property 'then' does not exist on type 'void'.
Am I missing something?
When you use the Union Type (denoted by the pipe character, '|'), you cannot assume the testFunction will return one type or another.
First you must check what type the testFunction returns after it has been called.
const result = testObject.testFunction()
if (result instanceof Promise) {
result.then(() => {});
}