redis/ioredis

"del" type regarding string and array of string

Open

#1 593 ouverte le 1 juin 2022

Voir sur GitHub
 (1 commentaire) (0 réactions) (0 assignés)TypeScript (12 302 stars) (1 069 forks)batch import
help wantedtyping

Description

Consider a function to remove a single key/array of keys:

public async removeKeys(keys: string | string[]): Promise<void> {
  this.redis.del(keys);
}

This currently gives a type error on the .del function since it cannot accept a union of string or string[].

I would expect that there is no type error since the code above simply works. Either passing a single key or an array of keys gives the correct result.

The current workaround is forcing the string to be an array, but that's weird to cast here just for typing.

public async removeKeys(keys: string | string[]): Promise<void> {
  keys = Array.isArray(userIds) ? userIds : [userIds]; // Needs explicit conversion here
  this.redis.del(keys);
}

What do you think? Is this the intended behavior or could we loosen/improve the typing here?

Guide contributeur