Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property finally is missing in type Promise

I'm pretty sure I'll be able to solve this issue by myself but if it can help somebody else I want to share and save somebody else time.

I had to add the es6-promise library to get rid of this error

Promise only result to a type, but is only used as a value here.

when I was trying to use Promise.all (see this discussion ). It worked fine until now, I get an error when I'm trying to use a Promise from MongoDB.

Promise<whatever> is not assignable to Promise<any>
    Property 'finally' is missing in type Promise<whatever>

According to this issue on es6-promise (if I got that right), the new finally property is breaking compatibility. It's there on the Promise Mongo return but not on the one I imported from es6-promise.

Any idea?

like image 457
Loic Coenen Avatar asked Jul 04 '18 13:07

Loic Coenen


2 Answers

I'm the one who noted that the finally shim was breaking Promise compatibility on that linked issue. Nice to see this getting some attention. Here are some options:

1. Rely purely on the TypeScript core library's typings

tsconfig.json

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

Install the shim, run the polyfill once at the start of your app, and from then on use the global Promise object rather than continuing to import the Promise class from es6-promise. I've found this to be the most inter-operable way.

npm install --save es6-promise@latest

Note that es6-promise has, for the last few releases, been bundling its own typings that conflict with the typings of TypeScript's built-in Promise libs.

2. Use an older version of es6-promise that doesn't include the finally shim

Note: Of course this means that you can't use finally

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

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

npm install --save [email protected] && npm install --save-dev @types/[email protected]

(Or otherwise omit those typings and add es2015.promise to your compilerOptions.lib array in tsconfig.json.)

like image 171
Jamie Birch Avatar answered Oct 23 '22 22:10

Jamie Birch


If all you need is a correct type definition for a Promise then you can just use the built-in definitions that come with typescript (I say this because you note that the promise you are actually working with has the finally method as expected).

To do this, remove es6-promise form you project and in tsconfig change your lib to:

{
    ....
    "lib": ["es5","es2015.promise","dom", "scripthost"] 
    ....
} 
like image 1
Titian Cernicova-Dragomir Avatar answered Oct 23 '22 20:10

Titian Cernicova-Dragomir