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

247
Views
TypeScript assign enum value to another enum value

I have a method called 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();
  };

In here `priority is a value which is equal to "Low", "Medium" or "High" Based on the priority I create an additional property called a label. The label could be "Low", "Medium" or "High"

In the else block when I try to say

label = priority

This gives me an error

enter image description here

How could I fix this issue?

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

0

It's not the cleanest design, but you can force the compiler's hand:

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!