redis / node-redis

Redis Node.js client
https://redis.js.org/
MIT License
16.95k stars 1.89k forks source link

HGET can return null, but TypeScript says it can't #2827

Open lantw44 opened 2 months ago

lantw44 commented 2 months ago

Description

Run the example code:

import { createClient, RedisClientOptions } from 'redis';

const options: RedisClientOptions = {};
const client = await createClient(options)
  .on('error', err => console.log('Redis Client Error', err))
  .connect();

const value = await client.HGET('hash', 'key');
console.log(value);

await client.disconnect();

It prints null if hash doesn't exist on the server. However, TypeScript says:

┌─────────────────────────────────────────────────┐
│const value: string | undefined                  │
└─────────────────────────────────────────────────┘

Which is wrong. TypeScript should remind the caller it should handle null.

Node.js Version

20.12.2

Redis Server Version

7.0.7 (Amazon ElastiCache)

Node Redis Version

4.7.0

Platform

Linux

Logs

No response

leibale commented 2 months ago

Oops... :facepalm: To fix it we should change the HGET transformReply function type to return RedisCommandArgument | null rather than RedisCommandArgument | undefined.

LMK if you want to create a PR, or if you prefer that I'll do that..