TypeScript type definition for promise.prototype.finally TypeScript type definition for promise.prototype.finally typescript typescript

TypeScript type definition for promise.prototype.finally


Whilst the answer from Slava is correct, it only deals with the typing of the finally block. To actually incorporate the shim into your code, so you can write p.finally(() => { ... }), you need to invoke the shim.

Unfortunately the typings on DefinitelyTyped do not currently support the shim function so until this is supported, I'd advise to add the typings yourself.

declare interface Promise<T> {  finally<U>(onFinally?: () => U | Promise<U>): Promise<U>;}declare module 'promise.prototype.finally' {  export function shim(): void;}

The types are now available. Install with

npm install --save-dev @types/promise.prototype.finally

In your TypeScript code, during application bootstrap, you can then call

import { shim } from 'promise.prototype.finally';shim();

This will add the finally block to the Promise prototype, allowing you to use finally as required.


For anyone wondering how to get this working natively without any shims: as of TS 2.7 it's possible.

Note that TS 2.7 is not yet fully (26. Feb. 2018) ES2018 compatible. While there are still a few things missing, Promise.finally made it into the 2.7 release. Also tsconfig-schema should already accept ES2018 as target but yet TS 2.7 has no knowledge of ES2018. For now to use the new features, such as Promise.finally, which already are in 2.7, you'll have to use "target": "ESNEXT" in your tsconfig.json.

Then you'll be able to write code like this:

myAsyncFunction().then(  (var) => console.log("executing then. " + var)).catch(  (var) => console.log("executing catch. " + var)).finally(  () => console.log("executing finally."))

Notice how finally will not take any arguments due to it's nature.

IMPORTANT: while TS will transpile correctly and understand what you're doing you still have to check if your JS-Engine supports Promise.finally. In my case I was using NodeJs 8.x and of course the produced JS was not executable because Promise.Finally is supported starting with in the latest NodeJs 10.x nightly builds Node 10.x (stable), see this link.


You can write your own d.ts file and reference it in the tsconfig.json file.Of you do it you can contribute to the DefinitelyTyped git for others like yourself

Update:

If I understood correctly what you mean you can extend the existing Promise class in your own d.ts file. Make the method to be optional so it won't tell you the actual Promise class is not implementing the interface correctly.You need to extend it as an interface.

Your d.ts file should look like this

declare module 'es6-promise/dist/es6-promise' {    export interface Promise <R> {      finally?<U>(onFinally?: () => U | Promise<U>): Promise<U>;    }}

And it should work properly...

I created a project for you as an example:promise-extension-typescript-example

I created a pull request to the DefinitelyTyped git repository, hope it will be accepted and you can download it from there...