gpt4 book ai didi

javascript - 如何正确处理 typescript 中的 promisifyAll?

转载 作者:数据小太阳 更新时间:2023-10-29 03:53:28 25 4
gpt4 key购买 nike

考虑以下代码:

import redis = require('redis');  //Has ambient declaration from DT
import bluebird = require('bluebird'); //Has ambient declaration from DT

bluebird.promisifyAll((<any>redis).RedisClient.prototype);
bluebird.promisifyAll((<any>redis).Multi.prototype);

const client = redis.createClient();

client.getAsync('foo').then(function(res) {
console.log(res);
});

getAsync 会出错,因为它是动态创建的,没有在任何 .d.ts 文件中定义。那么处理这个问题的正确方法是什么?

此外,即使我为 redis 加载了 .d.ts 文件,我仍然需要将 redis 转换为 any用于 promisifyAll。否则会溢出错误:

Property 'RedisClient' does not exist on type 'typeof "redis"'

输入 any 是唯一简单的方法吗?

最佳答案

我正在通过 declaration merging 解决这个问题setAsyncgetAsync 方法。我在自己的自定义 .d.ts 文件中添加了以下代码。

declare module "redis" {

export interface RedisClient extends NodeJS.EventEmitter {
setAsync(key:string, value:string): Promise<void>;
getAsync(key:string): Promise<string>;
}

}

关于javascript - 如何正确处理 typescript 中的 promisifyAll?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36563077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com