promise.prototype.finally 的 TypeScript 类型定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35876549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:20:31  来源:igfitidea点击:

TypeScript type definition for promise.prototype.finally

typescriptes6-promisedefinitelytyped

提问by jpierson

I was using this ES6 Promise compatible finally implementation called promise.prototype.finallyin a Node application that I want to convert to TypeScript however there are no typing available for this package that I can find on DefinitelyTyped. In these situations I've written up my own impromptu type definitions for just the subset of functionality that I need but in this case it is a library that modifies the prototype of the Promise object and I haven't encountered any conventional way to represent this in TypeScript. Any ideas?

我在一个 Node 应用程序中使用了这个 ES6 Promise 兼容的 finally 实现,称为promise.prototype.finally,我想将它转换为 TypeScript,但是我在绝对类型上找不到此包的可用类型。在这些情况下,我为我需要的功能子集编写了我自己的即兴类型定义,但在这种情况下,它是一个修改 Promise 对象原型的库,我还没有遇到任何传统的方式来表示这一点在打字稿中。有任何想法吗?

Possibly related:

可能相关:

回答by Tom Spencer

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

虽然 Slava 的答案是正确的,但它只涉及finally块的类型。要将 shim 实际合并到您的代码中,以便您可以编写p.finally(() => { ... }),您需要调用 shim。

Unfortunately the typings on DefinitelyTypeddo not currently support the shimfunction so until this is supported, I'd advise to add the typings yourself.

不幸的是,DefinitelyTyped 上的类型目前不支持该shim功能,因此在支持之前,我建议您自己添加类型。

declare interface Promise<T> {
  finally<U>(onFinally?: () => U | Promise<U>): Promise<U>;
}

declare module 'promise.prototype.finally' {
  export function shim(): void;
}
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

在您的 TypeScript 代码中,在应用程序引导期间,您可以调用

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

This will add the finallyblock to the Promiseprototype, allowing you to use finallyas required.

这会将finally块添加到Promise原型中,允许您finally根据需要使用。

回答by masi

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

对于想知道如何在没有任何垫片的情况下本地工作的人:从 TS 2.7 开始,这是可能的。

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.

请注意,TS 2.7 尚未完全(2018 年 2 月 26 日)与 ES2018 兼容。虽然仍然缺少一些东西,但Promise.finally 将其纳入 2.7 版本。此外 tsconfig-schema 应该已经接受 ES2018 作为目标,但 TS 2.7 不了解 ES2018。现在要使用新功能,例如 Promise.finally,已经在 2.7 中,您必须"target": "ESNEXT"在 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 argumentsdue to it's nature.

请注意,由于它的性质,finally 不会接受任何参数

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 buildsNode 10.x (stable), see this link.

重要提示:虽然 TS 会正确编译并理解你在做什么,但你仍然需要检查你的 JS-Engine 是否支持 Promise.finally。在我的情况下,我使用的是 NodeJs 8.x,当然生成的 JS 是不可执行的,因为Promise.Finally 从最新的 NodeJs 10.x nightly buildsNode 10.x (stable)开始支持Promise.Finally请参阅此链接

回答by Slava Shpitalny

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

您可以编写自己的 d.ts 文件并在 tsconfig.json 文件中引用它。如果你这样做了,你可以为像你这样的其他人贡献绝对类型的 git

Update:

更新:

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

如果我理解正确,您可以Promise在自己的 d.ts 文件中扩展现有类。使该方法成为可选的,这样它就不会告诉您实际的Promise类没有正确实现接口。您需要将其扩展为接口。

Your d.ts file should look like this

你的 d.ts 文件应该是这样的

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

我为你创建了一个项目作为例子: 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...

我创建了一个对绝对类型 git 存储库的拉取请求,希望它被接受,你可以从那里下载它......