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

246
Views
ENUM: Argument of type '"HH:MM"' is not assignable to parameter of type 'FormatOptions'

I made a function that accepts two arguments data and format. I'm trying to make ENUM(FormatOptions) for argument called "format". However, here is an error:

Argument of type '"HH:MM"' is not assignable to parameter of type 'FormatOptions'

How to write the correct ENUM for 2nd argument? Here is Playground on TypeScript Click

Code:

const basicTime: any = {
  year: 'numeric',
  month: 'short',
  day: '2-digit',
  hour: 'numeric',
  minute: 'numeric',
};

const hoursMinutes: any = {
  hour: 'numeric',
  minute: 'numeric',
};

enum FormatOptions {
  HoursMinutes = 'HH:MM',
  MonthDayYear = 'MM/DD/YYYY',
};

const dateFormat = (date: Date, format: FormatOptions) => {
  if (format === 'HH:MM') {
    return new Date(date).toLocaleString('en-US', hoursMinutes);
  }

  return new Date(date).toLocaleString('en-US', basicTime);
};

dateFormat(new Date, 'HH:MM');
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Instead of enum you can use union:

const basicTime: Intl.DateTimeFormatOptions = {
  year: 'numeric',
  month: 'short',
  day: '2-digit',
  hour: 'numeric',
  minute: 'numeric',
};

const hoursMinutes: Intl.DateTimeFormatOptions = {
  hour: 'numeric',
  minute: 'numeric',
};


type FormatOptions = 'HH:MM' | 'MM/DD/YYYY'


const dateFormat = (date: Date, format: FormatOptions) => {
  if (format === 'HH:MM') {
    return new Date(date).toLocaleString('en-US', hoursMinutes);
  }

  return new Date(date).toLocaleString('en-US', basicTime);
};

dateFormat(new Date(), 'HH:MM');

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!