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

175
Views
No puedo empujar a la matriz UnionType

Estoy jugando con UnionTypes en TS y pensé que podría crear un escenario válido donde podrían usarse, pero no entiendo por qué esto está fallando con el error de:

Argumento de tipo '{ nombre: cadena; }' no se puede asignar a un parámetro de tipo 'cadena & { nombre: cadena; }'

Conozco formas de "forzar" este enfoque particular, pero quiero entender por qué esto no funciona.

 let newArray: string[] | { name: string }[] = new Array(); //just a boolean to know when is one type or the other if (isMultiple) { otherArray.forEach((item: { name: string }) => { newArray.push(item) }) } else { otherArray.forEach((item: { name: string }) => { newArray.push(item.name) }); } return newArray
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

A menos que desee permitir matrices mixtas ( let newArray: (string | { name: string })[] ), puede usar

 // just a boolean to know when is one type or the other if (isMultiple) { const newArray: { name: string }[] = []; otherArray.forEach((item: { name: string }) => { newArray.push(item) }); return newArray; } else { const newArray: string[] = []; otherArray.forEach((item: { name: string }) => { newArray.push(item.name) }); return newArray; }
about 4 years ago · Juan Pablo Isaza Report

0

No puede tener ambos tipos de arreglos al mismo tiempo. Puede hacer esto y definirlo y luego crearlo por separado en la declaración if

 let newArray: string[] | { name: string }[]; //just a boolean to know when is one type or the other if (isMultiple) { newArray = new Array< { name : string }>(); otherArray.forEach((item: { name: string }) => { newArray.push(item) }) } else { newArray = new Array<string>(); otherArrayforEach((item: { name: string }) => { newArray.push(item.name) }); } return newArray
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!