0

In the Redis types definitions delete functions are not taking any arguments, and when I try to call them inside the NodeJS application I get

Expected 0 arguments, but got 1.ts(2554)

When calling

await redisClient.del(codeId);

I've fixed that by adding (args:string[]) to the types.d.ts file of @types/redis as a temporary fix, am I missing something here? Thanks in advance.

2
  • Hi Kingusha23, would you be able to provide more information? What type is redisClient? Are you using the redis package directly, or some promise-based wrapper around it? Commented Jun 11, 2020 at 9:58
  • Hello Paul, in my redis client file I do indeed use primisify wrapper: import redis, { RedisClient } from 'redis'; import { promisify } from 'util'; const client = redis.createClient({ host: process.env.REDIS_URL }); const redisClient = { ...client, removeAsync: promisify(client.del).bind(client), }; And then when I try to use await redisClient.removeAsync(id) - I get invalid arguments error Commented Jun 11, 2020 at 10:57

1 Answer 1

2

This is due to heavy function type overloading in the redis type definitions and a lack of support within the type declaration of promisify for overloaded functions.

The clean way to fix this would probably require TypeScript to merge support for variadic kinds, or if the redis package provided its own promisified API. Until that happens you can add typecasts to your code.

Approach 1: Cast the results of promisify

You can cast the result of promisify(client.del) to (string | string[]) => Promise<number> and do this for all other promisified redis functions you use.

Approach 2: Add a type overload for promisify

declare module "util" {
    function promisify<T, U, R>(fn: redis.OverloadedCommand<T, U, R>): {
        (arg1: T, arg2: T | T[]): Promise<U>;
        (arg1: T | T[]): Promise<U>;
        (...args: Array<T>): Promise<U>;
    };
}

This handles functions of type redis.OverloadedCommand, of which del is, but depending on how much of redis you use, you'd would probably also want to add additional overloads to promisify for OverloadedKeyCommand, OverloadedListCommand, OverloadedSetCommand, and OverloadedLastCommand.

Sign up to request clarification or add additional context in comments.

1 Comment

I just used expire function instead. But thanks for your explanation!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.