I just started programming a little bit with nodejs, typescript and redis. Unfortunately I run into a problem with redis: I define an interface of data to be stored in redis. However, as soon as I write the type behind the variable redis complains to me. Same when i get Keys from redis: How to tell Typescript which type the data is?
Example:
async function RedisQuestion() {
const redis = createClient();
interface userLogins {
token: string;
active: boolean;
}
interface myUser {
name: string;
id: string;
logins: userLogins[];
}
const objToStore: myUser = {
name: "Karl Mustermann",
id: "1",
logins: [
{
token: "1234",
active: true
},
{
token: "2345",
active: false
}
]
};
await redis.json.set("key", "$", objToStore);
const redisResult = redis.json.get("key") as myUser;
console.log(redisUser);
}
The way it works is if i say
await redis.json.set("key", "$", objToStore as any);
And if i get values from redis:
await redis.json.get("key") as any as myUser;
But there must be a better way?! Hope you can help :)
Thanks a lot