Categories
TypeScript Answers

How to fix the “Property ‘allSettled’ does not exist on type ‘PromiseConstructor’.ts(2339)” error with TypeScript?

Spread the love

To fix the "Property ‘allSettled’ does not exist on type ‘PromiseConstructor’.ts(2339)" error with TypeScript, we can add the allSettled method to the PromiseConstructor type definition.

For instance, we write

declare interface PromiseConstructor {
  allSettled(
    promises: Array<Promise<any>>
  ): Promise<
    Array<{ status: "fulfilled" | "rejected"; value?: any; reason?: any }>
  >;
}

to add the allSettled method into the PromiseConstructor interface.

We add the parameter with promises: Array<Promise<any>>.

And the return type is Promise< Array<{ status: "fulfilled" | "rejected"; value?: any; reason?: any }>.

Now we can use Promise.allSettled without compiler errors.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *