node-redis icon indicating copy to clipboard operation
node-redis copied to clipboard

Wrong return type of hGet

Open zdila opened this issue 8 months ago • 3 comments

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

zdila avatar Jun 04 '25 08:06 zdila

Its not just hGet(). It is every single function. Why is every function returning x | {}?

charlie-harvey-beable avatar Jul 02 '25 19:07 charlie-harvey-beable

I suspect it is due to String being a function and a Constructor.

jake-knerr avatar Jul 10 '25 14:07 jake-knerr

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.

jake-knerr avatar Jul 10 '25 15:07 jake-knerr