I expect type void[] to be compatible with type void. Specifically, when using Promise.all.
class Foo {
delete(): Promise<void> {
// do delete
return Promise.resolve();
}
deleteMany(list: Foo[]): Promise<void> {
return Promise.all(list.map((x) => x.delete()));
}
typescript error:
'Type 'Promise<void[]>' is not assignable to type 'Promise<void>'. Type 'void[]' is not assignable to type 'void'.'
I can solve this two ways that I know of:
- Mark deleteMany as returning Promise<void[]>.
Promise chain to Promise.all, to return a resolved promise. e.g.
return Promise.all(list.map((x) => x.delete())).then(() => Promise.resolve());
The second one is worse, since that bit of code executes in JavaScript, but the first one is confusing to developers. Does Typescript have poor support for Promise.all or is it omitted from their documentation? Anyone find a better solution?
deleteMany
as returningPromise<void[]>
? That's not poor support, that's what aPromise.all
call returns... – Heretic Monkey