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

Sometimes, we want to fix the “Property ‘allSettled’ does not exist on type ‘PromiseConstructor’.ts(2339)” error with TypeScript.

In this article, we’ll look at how to fix the “Property ‘allSettled’ does not exist on type ‘PromiseConstructor’.ts(2339)” error with TypeScript.

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

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.

Conclusion

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.