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

120
Views
Crear un tipo de matriz de múltiples objetos

Quiero crear un tipo para una matriz de objetos. La matriz de objetos puede verse así:

 const troll = [ { a: 'something', b: 'something else' }, { a: 'something', b: 'something else' } ];

el tipo que estoy tratando de usar es:

 export type trollType = [{ [key: string]: string }];

Entonces quiero usar el tipo como este:

 const troll: trollType = [ { a: 'something', b: 'something else' }, { a: 'something', b: 'something else' } ];

pero me sale este error:

 Type '[{ a: string; b: string; }, { a: string; b: string; }]' is not assignable to type 'trollType'. Source has 2 element(s) but target allows only 1

Puedo hacer algo como esto:

 export type trollType = [{ [key: string]: string }, { [key: string]: string }];

pero digamos que mi matriz de objetos tendrá 100 objetos en la matriz.

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

Al establecer un tipo para una matriz, debe tener este formato any[] .

Entonces en tu caso

 export type trollType = { [key: string]: string }[];
about 4 years ago · Santiago Gelvez Report

0

Puede intentar usar el tipo de Record para almacenar las definiciones de atributos del objeto y crear una matriz a partir de él, como se muestra a continuación:

 type TrollType = Record<string, string>[]; const troll: TrollType = [ { a: 'something', b: 'something else' }, { a: 'something', b: 'something else' } ];
about 4 years ago · Santiago Gelvez 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!