本文介绍了如何正确处理打字稿中的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 文件中定义。那么处理这个问题的正确方法是什么?
getAsync will error out because it's created on the fly and not defined in any .d.ts file. So what is the proper way to handle this?
此外,即使我有 .d.ts 文件为redis加载,我仍然需要将 redis 转换为任何以用于 promisifyAll 。否则,它会溢出错误:
Also, even though I have the .d.ts files loaded for redis, I still need to cast redis to any to be used for promisifyAll. Otherwise, it will spill out error:
Property 'RedisClient' does not exist on type 'typeof "redis"'输入任何唯一简单的方法?
推荐答案我正在通过声明合并 setAsync & getAsync 方法。我在自己的自定义 .d.ts 文件中添加了以下代码。
I'm solving this by declaration merging the setAsync & getAsync methods. I added the following code in my own custom .d.ts file.
declare module "redis" { export interface RedisClient extends NodeJS.EventEmitter { setAsync(key:string, value:string): Promise<void>; getAsync(key:string): Promise<string>; } }更多推荐
如何正确处理打字稿中的promisifyAll?
发布评论