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

304
Views
Texto mecanografiado que no puedo indexar en el objeto, que tiene muchos tipos de objetos
interface type1 { ttis: string, time: string } interface type2 { time: string, c2: number } type nameType1 = 'ttis' | 'time'; type nameType2 = 'c2' | 'time'; function testFun (v: type1[] | type2[], yKey: nameType1 | nameType2) { for (const item of v) { console.log(item[yKey]) // Property 'ttis' does not exist on type 'type1 | type2'. } }

La matriz v tiene muchos tipos de elementos que son un objeto, y quiero indexar estos objetos usando una yKey, pero el error siempre aparece

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Cree una restricción genérica para el tipo de valor y luego usekeyof para restringir las claves permitidas:

 interface Type1 { ttis: string, time: string } interface Type2 { time: string, c2: number } function testFun<T extends Type1 | Type2>(v: T[], yKey: keyof T) { for (const item of v) { console.log(item[yKey]); } }
about 4 years ago · Juan Pablo Isaza Report
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!