node.jstypescriptredisnode-promisify

How do I use HMSET with node promisify


I am using node (TypeScript) and node-redis library. Since I use TypeScript, I also imported @types/redis.

In addition, I read this article on how to promisify redis methods for TypeScript usage: https://flaviocopes.com/node-promisify/

My problem is that is that it works for some redis methods (like GET, SET, etc) but it does not work with e.g. HSET or HGETALL.

If I look into type suggestions, I see the critical methods are actually not methods at all, but rather properties (hset, HSET):

enter image description here

Why is this so? How can I access those needed methods?

I guess I am doing something obvious wrong. Any ideas?


Solution

  • I took a look at the source code of the typings and it seems that e.g. get is defined as a function:

    get(key: string, cb?: Callback<string | null>): R;
    

    whereas hset is defined as:

    hset: OverloadedSetCommand<string, number, R>;
    

    Altough OverloadedSetCommand is defined as

    export interface OverloadedCommand<T, U, R> {
        (arg1: T, arg2: T, arg3: T, arg4: T, arg5: T, arg6: T, cb?: Callback<U>): R;
        (arg1: T, arg2: T, arg3: T, arg4: T, arg5: T, cb?: Callback<U>): R;
        (arg1: T, arg2: T, arg3: T, arg4: T, cb?: Callback<U>): R;
        (arg1: T, arg2: T, arg3: T, cb?: Callback<U>): R;
        (arg1: T, arg2: T | T[], cb?: Callback<U>): R;
        (arg1: T | T[], cb?: Callback<U>): R;
        (...args: Array<T | Callback<U>>): R;
    }
    

    it seems that it's interpreted as a property (the same happens in Webstorm not only in vscode btw).

    If I manually override the definition as:

    hset(hash: string, field: string, value:any): R;
    

    the code completion shows a function as expected.

    Maybe you can raise an issue on https://github.com/DefinitelyTyped/DefinitelyTyped but for now I guess you have to just go with it.