hgetall: Type X does not satisfy the constraint 'Record<string, unknown>'
🎵 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
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.
Wait what? Why interface raises errors, but type does not? 🤯 🤦♂️
Weird TS quirk I suppose 😂
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.
This issue was closed because it has been stalled for 30 days with no activity.