Property finally is missing in type Promise

Loic Coenen picture Loic Coenen · Jul 4, 2018 · Viewed 9.1k times · Source

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?

Answer

Jamie Birch picture Jamie Birch · Jul 5, 2018

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.)