Skip to content
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

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

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

Comments

Projects
None yet
4 participants
@frogcjn
Copy link

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

This comment has been minimized.

Copy link
Author

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Author

commented May 20, 2016

@JoshGlazebrook Thanks!

Some Javascript magic trick still need dynamic.

@frogcjn frogcjn closed this May 20, 2016

@westy92

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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.
You can’t perform that action at this time.