Since TypeScript is a superset of Javascript and 'Type' would be removed in the resulting js files, so I guess playing with 'type' would not work like this, right? Also, is there any better way to do it other than having two different methods?
type VoidToVoidFunc = () => void;
type VoidToPromiseVoidFunc = () => Promise<void>;
async function add(func: VoidToVoidFunc | VoidToPromiseVoidFunc) {
if (typeof func == typeof VoidToVoidFunc)
func();
else
await func();
}
add(() => console.log(1));
As you can see here, when a non-thenable value follows
await, an already-fulfilledPromiseis constructed and used.So the simplest implementation is to
awaitthe result of the function call. Indeed,awaitwraps a value in aPromiseif it's not already aPromise:This behavior is correctly typed by TypeScript, indeed when you write
const test = await func();and hover the variable, its type is correctly inferred tovoid.TypeScript playground