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?
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]
wherecursor
isnumber
.EDIT: In fact, it turned out to be a bit more subtle. What I had to do is:
The problem is that the returned cursor is actually a
string
whereas on input side it is anumber
. Initially I was usingcursor === 0
as a termination condition, which caused an infinite loop due to the type mismatch. Perhaps the input argument toscan
should benumber | 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?