Redis:使用 TypeScript 添加到排序集

Redis: Add to sorted set using TypeScript

我正在尝试像这样使用 TypeScript 向 Redis 中的排序集添加一个值 client.ZADD('test', 10, 'test') 但这会引发错误

Argument of type '["test", 10, "test"]' is not assignable to parameter of type '[key: string, members: ZMember | ZMember[], options?: ZAddOptions | undefined] | [options: CommandOptions<ClientCommandOptions>, key: string, members: ZMember | ZMember[], options?: ZAddOptions | undefined]'.
  Type '["test", 10, "test"]' is not assignable to type '[options: CommandOptions<ClientCommandOptions>, key: string, members: ZMember | ZMember[], options?: ZAddOptions | undefined]'.
    Type at position 0 in source is not compatible with type at position 0 in target.
      Type 'string' is not assignable to type 'CommandOptions<ClientCommandOptions>'.
        Type 'string' is not assignable to type '{ readonly [symbol]: true; }'.

client.ZADD('test', { member: 'test', score: 10 }) 应该就可以了

在 redis 类型定义文件中找到:

node_modules\@node-redis\client\dist\lib\commands\generic-transformers.d.ts

类型 ZMember 的接口定义如下:

interface ZMember {
    score: number;
    value: string;
}

因此您的函数调用应如下所示:

client.zAdd('test_list', { score: 10, value: 'test'})