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

198
Vistas
How should I type these objects in typescript so that certain properties are restricted to objects with different ids?

I am relatively new to typescript and need some advice on how best to type an object structure like the one below. I want to be able to specify which item ids are allowed to be in a section based on a section id.

I realize this question might be vague, but really what I am looking for is for someone to point me in the right direction. If I need to totally re-type this object in a different way using some other typescript syntax, that is fine too. At this point, I am not sure what to Google or what I need to study about typescript to understand the best way to type an object like this.

Any help would be appreciated.

type ItemBase = {
  common: string | number
}

type Item = ItemBase & ({
  id: 'item1',
  custom1: string
} | {
  id: 'item2',
  custom1: number,
  custom2: string,
})

type SectionBase = {
  common: string | number
}

type Section = SectionBase & ({
  id: 'section1',
  items: {
    [key in Section1ItemIds]: Item
  },
} | {
  id: 'section2',
  items: {
    [key in Section2ItemIds]: Item
  },
})

type Section1ItemIds = 'item1';
type Section2ItemIds = 'item1' | 'item2';

type Group = {
  sections: {
    [key in Section['id']]: Section
  }
}

const group: Group = {
  sections: {
    section1: {
      id: 'section1',
      common: '',
      items: {
        item1: {
          id: 'item1',
          common: '',
          custom1: ''
        }
      }
    },
    section2: {
      id: 'section2',
      common: 0,
      items: {
        item1: {
          id: 'item1',
          common: '',
          custom1: ''
        },
        item2: {
          id: 'item2',
          common: '',
          custom1: 0,
          custom2: ''
        }
      }
    }
  }
}

const sectionIds = Object.keys(group.sections) as Array<keyof typeof group.sections>;
const sectionId = sectionIds[Math.floor(Math.random()*sectionIds.length)]
const section: Section = group[sectionId];

const getItemIds = (): (keyof typeof section.items)[] => {
  switch(section.id){
    case 'section1': return ['item1'];
    case 'section2': return ['item1','item2'];
  }
}
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