Skip to content

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

Open
@lantw44

Description

@lantw44

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions