如何正确处理打字稿中的promisifyAll?

戴夫

考虑以下代码:

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文件中定义那么解决这个问题的正确方法是什么?

此外,即使我有.d.ts加载redis的文件,我还需要投redisany用于promisifyAll否则,它将溢出错误:

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

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

戴夫

我被解决这一声明合并setAsyncgetAsync方法。我在自己的自定义.d.ts文件中添加了以下代码

declare module "redis" {

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

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章