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

196
Views
¿Cómo debo escribir estos objetos en mecanografiado para que ciertas propiedades estén restringidas a objetos con diferentes ID?

Soy relativamente nuevo en el mecanografiado y necesito algunos consejos sobre la mejor manera de escribir una estructura de objeto como la que se muestra a continuación. Quiero poder especificar qué identificadores de elementos pueden estar en una sección en función de un identificador de sección.

Me doy cuenta de que esta pregunta puede ser vaga, pero realmente lo que estoy buscando es que alguien me indique la dirección correcta. Si necesito volver a escribir totalmente este objeto de una manera diferente usando alguna otra sintaxis mecanografiada, también está bien. En este punto, no estoy seguro de qué buscar en Google o qué debo estudiar sobre mecanografiado para comprender la mejor manera de escribir un objeto como este.

Cualquier ayuda sería apreciada.

 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
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!