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

243
Views
TypeScript asigna valor de enumeración a otro valor de enumeración

Tengo un método llamado saveTask

 type Task = string; enum Priority { Low = "Low", Medium = "Medium", High = "High", } enum Label { Low = "Low", Med = "Med", High = "High", } interface Item { task: Task; priority: Priority; } const saveTask = ({ task, priority }: Item) => { const id = Date.now(); let label: Label; if (priority === "Medium") { label = Label.Med; } else { label = priority; } // dispatch(addItem({ task, priority, id, isActive: true, label })); // handleClose(); };

Aquí `priority es un valor que es igual a "Bajo", "Medio" o "Alto". Basado en la prioridad, creo una propiedad adicional llamada etiqueta. La etiqueta podría ser "Bajo", "Medio" o "Alto"

En el bloque else cuando trato de decir

etiqueta = prioridad

esto me da un error

ingrese la descripción de la imagen aquí

¿Cómo podría solucionar este problema?

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

0

No es el diseño más limpio, pero puedes forzar la mano del compilador:

 type Task = string; enum Priority { Low = "Low", Medium = "Medium", High = "High", } enum Label { Low = "Low", Med = "Med", High = "High", } interface Item { task: Task; priority: Priority; } const saveTask = ({ task, priority }: Item) => { const id = Date.now(); let label: Label; if (priority === Priority.Medium) { label = Label.Med; } else { label = Label[Priority[priority] as keyof typeof Label]; } console.log(label); }; saveTask({task: 'test', priority: Priority.Low}); // "Low" saveTask({task: 'test', priority: Priority.Medium}); // "Med" saveTask({task: 'test', priority: Priority.High}); // "High"
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!