node.jstypescriptredisioredis

Redis typescript delete method type casting


Hello i am wondering which type is right for delete functionality in redis. Code look like this.

/**
 * Delete a key
 * - _group_: generic
 * - _complexity_: O(N) where N is the number of keys that will be removed. When a key to remove holds a value other than a string, the individual complexity for this key is O(M) where M is the number of elements in the list, set, sorted set or hash. Removing a single key that holds a string value is O(1).
 * - _since_: 1.0.0
 */
del(...args: [...keys: RedisKey[], callback: Callback<number>]): Result<number, Context>;
del(...args: [keys: RedisKey[], callback: Callback<number>]): Result<number, Context>;
del(...args: [...keys: RedisKey[]]): Result<number, Context>;
del(...args: [keys: RedisKey[]]): Result<number, Context>;

So my function looks like this

 public async del(key: RedisKey[] | RedisKey) {
    return this._redis.del(key)
  }

Here is RedisKey declaration

export declare type RedisKey = string | Buffer;

For some reason it is not possible to pass string or array of strings at the same time.

Getting this error

TS2769: No overload matches this call.   Argument of type '[RedisKey | RedisKey[]]' is not assignable to parameter of type '[...keys: RedisKey[], callback: Callback<number>]'.     Type at position 0 in source is not compatible with type at position 1 in target.       Type 'RedisKey | RedisKey[]' is not assignable to type 'Callback<number>'.         Type 'string' is not assignable to type 'Callback<number>'.   Overload 2 of 4, '(...args: RedisKey[]): Promise<number>', gave the following error.     Argument of type 'RedisKey | RedisKey[]' is not assignable to parameter of type 'RedisKey'.       Type 'RedisKey[]' is not assignable to type 'RedisKey'.         Type 'RedisKey[]' is missing the following properties from type 'Buffer': write, toJSON, equals, compare, and 73 more.   Overload 3 of 4, '(keys: RedisKey[]): Promise<number>', gave the following error.     Argument of type 'RedisKey | RedisKey[]' is not assignable to parameter of type 'RedisKey[]'.       Type 'string' is not assignable to type 'RedisKey[]'.

error code

If i put just RedisKey[] or RedisKey it works as expected, but it won't work with both types. Please can someone elaborate what to do since there are 4 del() declarations, i am not sure what is going on? Function should accept both types just string or array of strings, so i want delete only one key or multiple keys in redis.


Solution

  • Process the parameter key and make parameters for redis.del() to match one of the overload signatures.

    import Redis, { RedisKey } from 'ioredis';
    
    const redis = new Redis();
    
    async function del(key: RedisKey[] | RedisKey) {
        const keys = Array.isArray(key) ? key : [key];
        return redis.del(keys);
    }
    
    del('key'); // ok
    del(['key1', 'key2']); //ok
    del('key1', 'key2'); // error
    

    Playground Link

    Or

    import Redis, { RedisKey } from 'ioredis';
    
    const redis = new Redis();
    
    async function del(...args: [...keys: RedisKey[]] | [keys: RedisKey[]]) {
        return redis.del(args.flat());
    }
    
    del('key');  // ok
    del(['key1', 'key2']); // ok
    del('key1', 'key2'); // ok
    

    Playground Link

    package version: "ioredis": "^5.3.2"