The Promise type is now incomplete without the property

I'm confident I can resolve this issue on my own, but if sharing my solution can save someone else time, then I want to do so.

To address the error, I needed to incorporate the es6-promise library.

The problem arose when utilizing Promise.all and encountering an issue regarding types. More details in <a href="https://stackoverflow.com/questions/43119163/typescript-error-ts2693-promise-only-refers-to-a-type-but-is-being-used-as#43122423">this discussion</a>.

Initially everything was functioning correctly, however, now I am facing an error specifically when working with a Promise from MongoDB.

Encountering an error stating: "Promise<whatever> is not assignable to Promise<any>. Property 'finally' is missing in type Promise<whatever>"

It seems that there may be compatibility issues related to the addition of a finally property on the Promise return from Mongo. This discrepancy does not exist in the version acquired from es6-promise, detailed in this es6-promise issue.

Any suggestions or solutions?

Answer №1

I was the one who initially pointed out that the finally shim was causing compatibility issues with Promises on the mentioned linked thread. It's great to see this problem getting some attention. Here are a couple of solutions:

1. Stick to TypeScript core library typings only

tsconfig.json

{
    "compilerOptions": {
        "lib": ["DOM","ES5","ScriptHost", "es2018.promise"]
    }
}

You can install the shim, execute the polyfill once at the beginning of your application, and then use the global Promise object instead of importing the Promise class from es6-promise. This method has proven to be the most compatible.

npm install --save es6-promise@latest

Please note that recent versions of es6-promise have been including conflicting typings that clash with TypeScript's built-in Promise libraries.

2. Go back to an earlier version of es6-promise without the finally shim

Keep in mind: This means you won't have access to finally

{
    "compilerOptions": {
        "lib": ["DOM","ES5","ScriptHost"]
    }
}

Install the previous version of es6-promise before they introduced finally, and use those typings:

npm install --save <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="046177322974766b696d776144302a362a36">[email protected]</a> && npm install --save-dev @types/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d3b6a0e5fea3a1bcbebaa0b693e3fde3fde0e1">[email protected]</a>

(Alternatively, you could skip those typings and add es2015.promise to your compilerOptions.lib array in tsconfig.json.)

Answer №2

If you are in need of an accurate type definition for a Promise, it is recommended to utilize the pre-existing definitions that are included with typescript. This suggestion is based on your mention of using a promise with the expected finally method.

To achieve this, simply eliminate es6-promise from your project and modify the lib in your tsconfig as follows:

{
    ....
    "lib": ["es5","es2015.promise","dom", "scripthost"] 
    ....
} 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to deactivate the error message related to "Unable to ascertain the module for component..."?

I recently incorporated a new component into my TypeScript2+Angular2+Ionic2 project. Right now, I have chosen not to reference it anywhere in the project until it is fully developed. However, there seems to be an error thrown by Angular/ngc stating "Cannot ...

Angular throwing an error message: "ChildrenOutletContexts provider not found!"

I developed a basic testing application and encountered the error message - "No provider for ChildrenOutletContexts!" I have searched through various related posts but to no avail. Here is my project structure: The App Module contains the App Routing Modu ...

Tips on saving a cookie using universal-cookie

I followed a solution on Stack Overflow to set a cookie in my React application. However, the cookie expires with the session. Is there a way I can make this cookie persist beyond the session so it remains even when the browser is closed and reopened? ex ...

What is the reason behind the lack of covariance in an interface when using a type of T[keyof T]?

I'm attempting to enable type covariance so that Type<Cat> can be treated as a Type<Animal>, for instance, treating a list of Cats as a list of Animals. However, using the type T[keyof T] in a method within the Type interface seems to hind ...

Having trouble setting default value using formControl in a component shared across multiple forms

Currently, I am facing an issue with setting a default value in a custom dropdown by using formControlName. In order to attach formControls to a shared component, I am utilizing ControlValueAccessors in the shared component. This allows me to update the fo ...

Creating a Jsonifiable type that aligns with static interfaces: A step-by-step guide

When working with Typescript, there are 3 types that share similarities as they are composed of primitive types, maps, and arrays: type Color1 = { [component: string]: number } type Color2 = { green: number red: number blue: number } interface Colo ...

Are there alternative methods to navigate in Angular 6 that are similar to the navigation in Ionic 3?

I'm currently facing an issue with navigation in Angular 6. In Ionic, we can navigate to a different page by using navCtrl.push('ExamplePage'); However, in Angular 6, I am having trouble navigating through a button click like this: app.co ...

Utilize React to display a Selected button within a whitespace

Currently, I am encountering an issue in React where I have a group of buttons at the bottom. Upon selecting a button, the corresponding text should display at the top within a specified whitespace. However, I am looking to have the whitespace already occu ...

What does an exclamation mark signify in Angular / Type Script?

Being a newcomer in the world of front end development, I am currently teaching myself Angular (11.2.10). While exploring a sample project, I noticed this particular piece of html template code written by another person and utilized in multiple places: < ...

What is the best way to exempt a unique situation from a directive's operation?

While troubleshooting a bug related to search functionality on my page, I encountered an issue with the search component. The search feature is working correctly and returning the expected values. However, when I clear the search criteria, I noticed that t ...

`Angular2 Reactively-shaped Form Elements with BehaviorSubject`

As a newcomer to Angular, I am struggling with updating reactive forms after making asynchronous calls. My specific challenge involves having a reactive form linked to an object model. Whenever there is a change in the form, it triggers an HTTP request th ...

Error in parsing: Unexpected token encountered. Expected a comma instead. Issue found in React with Typescript

I'm encountering a new error message that I haven't seen before... I've checked my code thoroughly and it seems to be correct, yet the error persists. Here is my code snippet: interface AuthState { token: string; user: User; } interfac ...

Tips for retrieving input values when they are not available in HTML documents

In this code snippet, I am creating an input element with the file type without adding it to the DOM. My goal is to retrieve the value of this input once the user selects a file. Even though the input is not part of the HTML template, I still want to acces ...

Using node.js to set the billing address while confirming a Stripe Payment intent

Is there a way to specify the billing address of a payment intent with Node.js? When using the Stripe.js browser-side framework, I can easily accomplish this by utilizing Stripe elements and the stripe.confirmPayment function, which automatically captures ...

Displaying a blank array after populating it with data using a for loop within an asynchronous function

Just starting to learn about async/await. I'm facing an issue where when I try to print an array using console.log, it shows an empty array [], but within the loop, the data is displayed correctly. Can anyone offer some guidance on what might be causi ...

Issues arise with Jest tests following the implementation of the 'csv-parse/sync' library

Currently utilizing NestJs with Nx.dev for a monorepo setup. However, I've come across an issue after installing csv-parse/sync - my Jest tests are now failing to work. Jest encountered an unexpected token Jest failed to parse a file due to non-stand ...

Importing custom pipes in Angular 2 becomes a bit tricky when working with data grouping

As a newcomer to Angular JS, I have recently started using Angular 2 for a project of mine. Here is an example of my JSON data: "locations": [ { "id": "ASS", "name": "test center", "city": "Staten Island", "zip": ...

Ways to obtain the file path of the compiled worker.js loaded from the worker loader, along with its hash

Currently, I am working on a TypeScript React application that utilizes Babel and Webpack for compilation. I have implemented a rule to load my worker with the following configuration: config.module.rules.unshift({ test: /gif\.worker\.js$/, ...

Enhancing Angular 6 with Constructor Overloading

I'm a newcomer to TypeScript/Angular and I have a constructor set up to fetch JsonP. Now, I want to add a new constructor for HttpClientModule. Here's my current code: export class DataService { constructor(private _jsonp: Jsonp) { t ...

Ways to update the state of an array without clearing the existing array items

I am attempting to add fetched array items to an existing state that already contains items (with plans to include pagination). However, when I try using code similar to setMovies((prevMovies) => [...prevMovies, ...arr1]), I encounter a Typescript erro ...