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

hgetall: Type X does not satisfy the constraint 'Record<string, unknown>'

Open ackvf opened this issue 2 years ago • 4 comments

🎵 The type constraint seems a bit too constraining. It is obviously correct, but I can't type it to type it. Hope it makes sense. KTHXBYE 🎵

The error:

Type 'Receipt' does not satisfy the constraint 'Record<string, unknown>'.
  Index signature for type 'string' is missing in type 'Receipt'.ts

Code:

interface Receipt {
  time: string
  user: string
  hash: string
}

// @ts-ignore
const data = await kv.hgetall<Receipt>('some:key')
//                            ^ type error (see above)

// data is inferred as Receipt | null
data?.time // "2024-02-13T21:18:07.013Z"   //   🗸
data?.user // "1234"   //   🗸
data?.hash// "xyz"   //   🗸

REDIS CLI

> hgetall some:key
hash, xyz, user, 1234, time, 2024-02-13T21:18:07.013Z

ackvf avatar Feb 13 '24 21:02 ackvf

Hey, it's indeed a weird issue. This works:

type Receipt = {
  time: string;
  user: string;
  hash: string;
};

const redis = new Redis()
redis.hgetall<Receipt>("test-test");

//OR

interface Receipt {
  time: string;
  user: string;
  hash: string;
  [key: string]: unknown; // Allows additional string keys with unknown values
}


const redis = new Redis()
redis.hgetall<Receipt>("test-test");

I'll see what I can do, but for now you can use one of the ways I provided.

ogzhanolguncu avatar Feb 14 '24 07:02 ogzhanolguncu

Wait what? Why interface raises errors, but type does not? 🤯 🤦‍♂️

ackvf avatar Feb 20 '24 23:02 ackvf

Weird TS quirk I suppose 😂

ogzhanolguncu avatar Feb 21 '24 07:02 ogzhanolguncu

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Mar 23 '24 01:03 github-actions[bot]

This issue was closed because it has been stalled for 30 days with no activity.

github-actions[bot] avatar Apr 22 '24 01:04 github-actions[bot]