node-redis
node-redis copied to clipboard
Wrong return type of hGet
Description
export const redisClient = createClient(options).withTypeMapping({
[RESP_TYPES.BLOB_STRING]: String,
});
// Type 'string | {}' is not assignable to type 'string'. Type '{}' is not assignable to type 'string'.ts(2322)
const res: string = await redisClient.hGet("foo", "bar");
Typescript Version
5.8.3
Node Redis Version
@redis/client 5.5.5
Its not just hGet(). It is every single function. Why is every function returning x | {}?
I suspect it is due to String being a function and a Constructor.
This worked for me:
.withTypeMapping({
[RESP_TYPES.BLOB_STRING]: (val) => (val === null ? null : val.toString()),
[RESP_TYPES.BLOB_ERROR]: (val) => (val === null ? null : val.toString()),
})
Providing a custom decoder instructs Typescript to infer string | null instead of the string and the String constuctor object.