I have the following piece of code that results in compile error in TypeScript:
type Promisable = (() => Promise<string>) | (() => Promise<number>);
const func = async (promisable: Promisable) => {
await Promise.all([promisable()]);
};
The error is as follows
No overload matches this call. The last overload gave the following error. Argument of type '(Promise | Promise)[]' is not assignable to parameter of type 'Iterable>'. The types returned by 'Symbol.iterator.next(...)' are incompatible between these types.
For the record, removing the union type works as intended:
type Promisable = () => Promise<string>;
const func = async (promisable: Promisable) => {
await Promise.all([promisable()]);
};
You can see the error for yourself here https://www.typescriptlang.org/play/?ssl=4&ssc=3&pln=1&pc=1#code/C4TwDgpgBACgTgewLYEsDOBDARgG2gXigAoiBKKfAPlkVTQgB41g4UA7Ac0vIB9iyK1eMnSM2AVyRYIcbgG4AsACgAxgjbMoAM3FsVFKBjQg9xMLXTY8ALhojMuCOSpQA3sqiGA7hhTA7dBAAdBg4OEQA2ub2VhBkALqkikoAvnJAA
Is it not possible to use union types in combination with Promise.all
?
EDIT:
I know it's possible to use something like () => Promise<string|number>
instead. But in an advanced application with a lot of asynchronous functions and big types, it is not easy to convert union of functions into function of union. It's not very practical from the code perspective as well.