Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

70
Views
how do I use the key of an object inside the object in typescript?

I am trying to design an interface for postComments, it looks like this:

interface PostComments {
    postId:{
        id: string;
        comment: string;
        postId: string;
    }[]
}

I want the compiler to give me an error when the postId's are not the same

the progress that I made so far is:

interface Comment<T> {
    id: string;
    comment: string;
    postId: T
}

interface PostComments {
    [postId:string]:Comment<PostComments.postId>[];
}

but unlucky me, typescript is not happy with the interface that I wrote, how can I throw a compile time exception for this? please let me know if someone have an idea about it.

=========================================================================

i want the comments object to look like:

comments = {
    "abc123": [
        {
            id: "xyz098",
            comment: "a sample comment",
            postId: "abc123",
        },
        {
            id: "xyz987",
            comment: "another sample comment",
            postId: "abc123",
        },
    ],
    "bcd123": [
        {
            id: "yxz098",
            comment: "a new sample comment",
            postId: "bcd123",
        },
        {
            id: "yxz987",
            comment: "another new sample comment",
            postId: "bcd123",
        },
    ]
}
  • notice the keys in comment is same as that of postId in every array element of the corrosponding post
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!