考虑以下代码:
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的文件,我还需要投redis
以any
用于promisifyAll
。否则,它将溢出错误:
Property 'RedisClient' does not exist on type 'typeof "redis"'
键入它any
是唯一简单的方法吗?
我被解决这一声明合并了setAsync
与getAsync
方法。我在自己的自定义.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] 删除。
我来说两句