Specify types on read and write operations
When I simply run an operation that reads or writes, for instance, get & set/ push
can I simply provide what is expected through generic types ?
Like redis.get<ClassA>(key)
or
const objs: ClassB[] =[{}] // example
const redis.lpush<ClassB>(key, objs)
Thanks a lot
It doesn't make sense to support generics as lpush always accept strings. What's your use case?
Sorry, my bad you are right.
But what about a function that serialize and de- serialize strings and use the generic version of the API.
I'm sure in most cases people do JSON.stringify(obj) before insertion and JSON.parse(str) after getting values.
I myself keep doing this.
I think it would make more sense in this situation:
interface UserProfile {
id: string;
username: string;
}
const userId = "ab4dfe5"
const user = await redis.hgetall<UserProfile>(userId)
// ^? {} | UserProfile
const user = await redis.hgetall(userId)
// ^? {} | Record<string, string>
However it wouldn't be feasible as number fields are returned as strings so unless a serializer is provided this can not happen