Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lantw44 opened this issue Sep 5, 2024 · 1 comment
Open

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

lantw44 opened this issue Sep 5, 2024 · 1 comment
Labels

Comments

@lantw44
Copy link

lantw44 commented Sep 5, 2024

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

@lantw44 lantw44 added the Bug label Sep 5, 2024
@leibale
Copy link
Collaborator

leibale commented Sep 19, 2024

Oops... 🤦
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..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants