Skip to content

How to let TypeScript work with bluebird promisifyAll or promisify? #8685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
frogcjn opened this issue May 19, 2016 · 5 comments
Closed

How to let TypeScript work with bluebird promisifyAll or promisify? #8685

frogcjn opened this issue May 19, 2016 · 5 comments

Comments

@frogcjn
Copy link

frogcjn commented May 19, 2016

import * as redis from "redis";
import * as Promise from "bluebird";
Promise.promisifyAll(redis);
const redisClient = redis.createClient(); 
redisClient.hexistsAsync("myhash", "field").then(function(v) {

}).catch(function(e) {

});

This code will not work with TypeScript, since it has not enough type information for the promisified redis.

@frogcjn
Copy link
Author

frogcjn commented May 19, 2016

Or should I use RxJS instead of bluebird?
I think it is better to use RxJS, since it is typescript based.

@JoshGlazebrook
Copy link

JoshGlazebrook commented May 20, 2016

You would need to do:

import * as Promise from 'bluebird';
import * as redis from 'redis';
const redisAsync: any = Promise.promisifyAll(redis);

There won't be any intellisense, but it will let you call .xxxAsync().

@frogcjn
Copy link
Author

frogcjn commented May 20, 2016

@JoshGlazebrook Thanks!

Some Javascript magic trick still need dynamic.

@frogcjn frogcjn closed this as completed May 20, 2016
@westy92
Copy link

westy92 commented Aug 16, 2016

How about:

import * as Promise from 'bluebird';
import * as Redis from 'redis';

declare module 'redis' {
  export interface RedisClient extends NodeJS.EventEmitter {
    hdelAsync(...args: any[]): Promise<any>;
    // add other methods here
  }
}

const oldRedisClient = Redis.createClient(...);
const redisClient = Promise.promisifyAll(oldRedisClient) as Redis.RedisClient; // cast

// redisClient.hdelAsync now shows up in intelliSense.

@tomyam1
Copy link

tomyam1 commented Aug 9, 2017

How about

import * as Bluebird from 'bluebird';
import * as AWS from 'aws-sdk';

export interface IPromisifedS3 extends AWS.S3 {
    [x: string]: any
}

const s3 = new AWS.S3();
Bluebird.promisifyAll(s3);

export default s3 as IPromisifedS3;

This enables type checking and auto completion for the normal functions while allowing to use the promosified methods

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants