Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

72
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda