redis/ioredis

"del" type regarding string and array of string

Open

#1,593 建立於 2022年6月1日

在 GitHub 查看
 (1 留言) (0 反應) (0 負責人)TypeScript (12,302 star) (1,069 fork)batch import
help wantedtyping

描述

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?

貢獻者指南