Skip to content

Fix: zremrangebyscore should accept string | number for min/max scores #1395

@mvonschledorn

Description

@mvonschledorn

Description

The zremrangebyscore command has incorrect TypeScript type definitions that only accept number for min and max parameters, but the Redis protocol (and Upstash REST API) accept string values like '-inf' and '+inf'.

Current Type Definition

export class ZRemRangeByScoreCommand extends Command<number, number> {
  constructor(cmd: [key: string, min: number, max: number], opts?: CommandOptions<number, number>)
}

Expected Type Definition

export class ZRemRangeByScoreCommand extends Command<number, number> {
  constructor(cmd: [key: string, min: number | string, max: number | string], opts?: CommandOptions<number, number>)
}

Justification

  1. Redis Protocol Support: Redis ZREMRANGEBYSCORE accepts special string values:

    • '-inf' for negative infinity
    • '+inf' for positive infinity
    • '(value' for exclusive ranges
  2. Consistency with Other Commands: The ZCountCommand already uses min: number | string, max: number | string (see zcount.ts), making this an inconsistency in the codebase.

  3. Runtime vs Types Mismatch: The Upstash REST API already accepts these string values at runtime, but TypeScript types force developers to use type assertions.

Evidence

This works at runtime but requires a type workaround:

// Current workaround required
// @ts-expect-error - Upstash types only show number, but runtime accepts strings
await redis.zremrangebyscore('mykey', '-inf', Date.now() - 900000);

// OR with String() conversion
await redis.zremrangebyscore('mykey', String('-inf'), String(Date.now() - 900000));

Proposed Fix

Update type definitions in pkg/commands/zremrangebyscore.ts:

export class ZRemRangeByScoreCommand extends Command<number, number> {
-  constructor(cmd: [key: string, min: number, max: number], opts?: CommandOptions<number, number>) {
+  constructor(cmd: [key: string, min: number | string, max: number | string], opts?: CommandOptions<number, number>) {
    super(["zremrangebyscore", ...cmd], opts);
  }
}

References


I'm preparing a PR to fix this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions