Type Void [] An array not assigned to the type Void

I expect the void [] type to be compatible with the void type. In particular, 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 assigned to the type "Promise <void>". The type 'void []' is not assigned to the type 'void'. ''

I can solve these two ways that I know of:

  • Mark deleteMany as returning Promise <void []>.
  • Share the Promise.all chain to return the promised promise. eg.

    return Promise.all(list.map((x) => x.delete())).then(() => Promise.resolve());
    

The second one is worse because this bit of code is executed in JavaScript, but the first one confuses developers. Does Typescript have poor support for Promise.all or is it not listed in their documentation? Anyone find a better solution?

+4
2

1 ( deleteMany Promise<void[]>) , , .

async/await, Promise<void>:

class Foo {
  delete(): Promise<void> {
    // do delete
    return Promise.resolve();
  }
  async deleteMany(list: Foo[]): Promise<void> {
    await Promise.all(list.map((x) => x.delete()));
  }

.

+4

void , null undefined , void[].

Promise<{}> deleteMany, . , ( - ), (), , void[]:

class Foo {
    delete(): Promise<void> {
        // do delete
        return Promise.resolve();
    }
    deleteMany(list: Foo[]): Promise<{}> {
        return Promise.all(list.map((x) => x.delete()));
    }
}
+2

Source: https://habr.com/ru/post/1675565/


All Articles