Thursday, May 9, 2024
 Popular · Latest · Hot · Upcoming
94
rated 0 times [  101] [ 7]  / answers: 1 / hits: 6458  / 8 Years ago, tue, april 12, 2016, 12:00:00

Consider the following code:



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 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?



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'


Is typing it to any the only easy way to go?


More From » typescript

 Answers
3

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>;
}

}

[#29522] Sunday, April 10, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marcos

Total Points: 331
Total Questions: 106
Total Answers: 104

Location: Gabon
Member since Sat, Jul 25, 2020
4 Years ago
;