简体   繁体   English

有没有办法在打字稿上使用 util.promisify 继承方法签名?

[英]Is there any way to inherit method signature with util.promisify on typescript?

From v8.0.0 Node provides util.promisify() API.v8.0.0 Node 开始提供util.promisify() API。 Now I'm trying to convert some callback-style method into async/await style.现在我正在尝试将一些回调风格的方法转换成 async/await 风格。 On typescript, util.promisify() may not inherit method signature:在打字稿上, util.promisify()可能不会继承方法签名:

import fs = require('fs');

export namespace AsyncFs {
    export const lstat = util.promisify(fs.lstat);
    // there's no method signature, only shows as "Function"
}

Although we can add new signature for each method...虽然我们可以为每个方法添加新的签名......

export const lstat = util.promisify(fs.lstat) as (path: string | Buffer) => fs.Stats;

So I'm looking for a good way to inherit signatures automatically.所以我正在寻找一种自动继承签名的好方法。 Is it possible?是否可以? Do you have any good ideas?你有什么好主意吗?

Thanks.谢谢。

If not handled by TS internally, then you will likely have to define the type for util.promisify() yourself doing something similar to what they do for Bluebird's promisify() static function in DefinitelyTyped .如果不是由 TS 内部处理,那么您可能必须自己定义util.promisify()的类型,执行类似于他们在绝对类型中为 Bluebird 的 promisify() 静态函数所做的事情。

  static promisify<T>(func: (callback: (err: any, result: T) => void) => void, options?: Bluebird.PromisifyOptions): () => Bluebird<T>;
  static promisify<T, A1>(func: (arg1: A1, callback: (err: any, result: T) => void) => void, options?: Bluebird.PromisifyOptions): (arg1: A1) => Bluebird<T>;
  static promisify<T, A1, A2>(func: (arg1: A1, arg2: A2, callback: (err: any, result: T) => void) => void, options?: Bluebird.PromisifyOptions): (arg1: A1, arg2: A2) => Bluebird<T>;
  static promisify<T, A1, A2, A3>(func: (arg1: A1, arg2: A2, arg3: A3, callback: (err: any, result: T) => void) => void, options?: Bluebird.PromisifyOptions): (arg1: A1, arg2: A2, arg3: A3) => Bluebird<T>;
  static promisify<T, A1, A2, A3, A4>(func: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any, result: T) => void) => void, options?: Bluebird.PromisifyOptions): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Bluebird<T>;
  static promisify<T, A1, A2, A3, A4, A5>(func: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any, result: T) => void) => void, options?: Bluebird.PromisifyOptions): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Bluebird<T>;

In cases where the type system fails to infer the promisified function signature, you can provide type hints using generics:在类型系统无法推断出promisified 函数签名的情况下,您可以使用泛型提供类型提示:

import { promisify } from 'util';
import jsonwebtoken from 'jsonwebtoken';
import type { VerifyOptions, Secret } from 'jsonwebtoken';


const verify = promisify<string, Secret, VerifyOptions | undefined, JwtExtendedPayload>(jsonwebtoken.verify);

which would yield the result below:这将产生以下结果:

const verify: (arg1: string, arg2: jsonwebtoken.Secret, arg3: jsonwebtoken.VerifyOptions | undefined) => Promise<JwtExtendedPayload>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NodeJs util.promisify 不是函数 - NodeJs util.promisify is not a function TypeError:util.promisify不是函数吗? - TypeError: util.promisify is not a function? 节点:util.promisify() 没有回调作为最终参数 - Node: util.promisify() without callback as final parameter 模块“util”已被外部化以实现浏览器兼容性。 无法在客户端代码中访问“util.promisify” - Module "util" has been externalized for browser compatibility. Cannot access "util.promisify" in client code 有没有办法宣传节点的功能并通过Typescript获得智能感知? - Is there any way to promisify node's functions and get intellisense with Typescript? &#39;util.promisify(setTimeout)&#39; 和 &#39;ms =&gt; new Promise(resolve =&gt; setTimeout(resolve, ms))&#39; 之间的区别 - difference between 'util.promisify(setTimeout)' and 'ms => new Promise(resolve => setTimeout(resolve, ms))' Continue.map 循环后 util.promisify 请求迭代失败(异步等待) - Continue .map loop after util.promisify request fails in iteration (async await) Node.js util.promisify-将stdout输出为obj或数组而不是换行 - Node.js util.promisify - Output stdout as obj or array instead of new lines 如何通过writeFile fs函数使用节点js util.promisify并等待 - How to use node js util.promisify with the writeFile fs function and await Postgres:何时在查询中使用.end() vs.release()(这在 util.promisify 中是否发生了变化)? 缺少这些功能是否会增加 memory 的使用量? - Postgres: When to use .end() vs .release() in queries (and does this change in util.promisify)? Does lack of these functions increase memory usage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM