redis/ioredis

"del" type regarding string and array of string

开放

#1,593 创建于 2022年6月1日

 (1 条评论) (0 个反应) (0 位负责人)TypeScript (1,069 个派生)batch import
help wantedtyping

仓库指标

星标
 (12,302 个星标)
PR 合并指标
 (平均合并 3天 5小时) (30 天内合并 5 个 PR)

描述

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?

贡献者指南