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

编程入门 行业动态 更新时间:2024-10-19 02:16:47
本文介绍了如何正确处理打字稿中的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?

本文发布于:2023-11-24 04:38:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623999.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确处理   promisifyAll

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!