Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hmset in typescript object #1536

Closed
sumanadak opened this issue Mar 22, 2022 · 9 comments · Fixed by #1545
Closed

hmset in typescript object #1536

sumanadak opened this issue Mar 22, 2022 · 9 comments · Fixed by #1545

Comments

@sumanadak
Copy link

I tried to insert one simple object in typescript to ioredis hashmap. I define object like this

interface User {
name : string,
age :string
}

let newUser : User = {
name : "suman",
age :"34"
}
How do I write hmset command ?

@luin
Copy link
Collaborator

luin commented Mar 23, 2022

There's an example in README: https://github.com/luin/ioredis/blob/main/examples/hash.js#L13. Prior v5.x.x, you also need to install @types/ioredis since v5.0.0 is the first version ioredis provides native TypeScript declarations.

@luin luin added the question label Mar 23, 2022
@sumanadak
Copy link
Author

Thanks a lot. I have seen this example . Perhaps I am not good with typescript, hence I am not able convert same example to typescript object type defination and call the API.

Could you please give an example which is written with typescript ?

@sumanadak
Copy link
Author

I tried like this

Redis.hmset("user-hash", newUser) but typescript has an error...What could be the right way to put newUser object ?

@luin
Copy link
Collaborator

luin commented Mar 24, 2022

const redis = new Redis();

redis.hmset("hash", {
  name: "Bob",
  age: "20",
});

It works for me. Redis is a constructor so you need to follow the example to create an instance. Does it solve your issue?

@luin
Copy link
Collaborator

luin commented Mar 26, 2022

Closing as this should be resolved in v5.0.0. Feel free to reopen if the issue still exists.

@luin luin closed this as completed Mar 26, 2022
@sumanadak
Copy link
Author

sumanadak commented Mar 26, 2022

dear luin,
I am sorry for late reply. I was not in town. Here is my problem. I created constructor of redis and tried pass this typescript object like this
interface User {
name : string,
age :string
}

let newUser : User = {
name : "suman",
age :"34"
}
let redis = new Redis()
when I try to do like this redis.hmset("user-hash",newUser)
it gives me a error. Asking either string or map. Cant we pass object like this ? May be I am doing something wrong. Can you please give me some example with typescript object with hmset as example ?

@luin
Copy link
Collaborator

luin commented Mar 27, 2022

Thanks for the follow-up. It's actually a limit on TypeScript side that you can't assign an interface to a record:

interface User {
  name: string;
  age: string;
}

let newUser: User = {
  name: "suman",
  age: "34",
};

const record: Record<string, string> = newUser; // Error here

ioredis expects a Record for the parameter so that's why you got the error. I'm working on making ioredis accept any interface as well.

@luin luin reopened this Mar 27, 2022
github-actions bot pushed a commit that referenced this issue Mar 30, 2022
## [5.0.2](v5.0.1...v5.0.2) (2022-03-30)

### Bug Fixes

* allow option maxRetriesPerRequest to be null ([#1553](#1553)) ([d62a808](d62a808)), closes [#1550](#1550)
* support TypeScript interface as parameters of hmset and mset ([#1545](#1545)) ([3444791](3444791)), closes [#1536](#1536)
@github-actions
Copy link

🎉 This issue has been resolved in version 5.0.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@sumanadak
Copy link
Author

Dear luin,
Thanks a lot for this release.

Thanks
Suman

janus-dev87 added a commit to janus-dev87/ioredis-work that referenced this issue Mar 1, 2024
## [5.0.2](redis/ioredis@v5.0.1...v5.0.2) (2022-03-30)

### Bug Fixes

* allow option maxRetriesPerRequest to be null ([#1553](redis/ioredis#1553)) ([d62a808](redis/ioredis@d62a808)), closes [#1550](redis/ioredis#1550)
* support TypeScript interface as parameters of hmset and mset ([#1545](redis/ioredis#1545)) ([3444791](redis/ioredis@3444791)), closes [#1536](redis/ioredis#1536)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants