Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to define a type for an object with and id

I need to define an interface that reflects this part of a response:

"responses": {
    "accepted": [],
    "declined": [],
    "messages": {
        "id1": {
            "profile": "",
            "message": "Lorem ipsum"
        },
        "id2": {
            "profile": "",
            "message": "Lorem ipsum"
        }
     }

So far I have defined the responses like this:

export interface Response {
    accepted: string[],
    declined: string[],
    messages: Messages
}

How do I define the Messages interface properly?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

export interface Messages {
    profile: string,
    message: string,
}

>Solution :

You forgot to add string keys to messages, and rename Messages to Message to reflect that this is one object:

export interface Response {
    accepted: string[],
    declined: string[],
    messages: Record<string, Message>
}
export interface Message {
    profile: string,
    message: string,
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading