mmkal / handy-redis

A wrapper around node_redis with Promise and TypeScript support.
Apache License 2.0
172 stars 10 forks source link

Scan has `unknown` return type #288

Closed bluenote10 closed 3 years ago

bluenote10 commented 3 years ago

I'm trying to use SCAN (and its variants) in handy-redis, but I'm a bit confused that it has an unknown return type. I'll experiment with casting, assuming that I may have to cast it to a tuple of [cursor, results] where cursor is number.

EDIT: In fact, it turned out to be a bit more subtle. What I had to do is:

type ScanReturnType = [string, string[]];

export async function scanTest(redis: RedisClient) {
  let prevCursor = "0";
  while (true) {
    let [cursor, data] = (await redis.scan((prevCursor as unknown) as number)) as ScanReturnType;
    console.log(data.length);
    if (cursor === "0") {
      break;
    } else {
      prevCursor = cursor;
    }
  }
}

The problem is that the returned cursor is actually a string whereas on input side it is a number. Initially I was using cursor === 0 as a termination condition, which caused an infinite loop due to the type mismatch. Perhaps the input argument to scan should be number | string so that reusing the cursor is more straightforward and avoids the (prevCursor as unknown) as number) cast.

Would it make sense to adjust the types to make that more convenient?