Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

675
Vistas
Typescript: Type 'string' is not assignable to type '"numeric" | "2-digit"' in Date::toLocaleDateString()

I have recently been alerted to an "error" by Visual Studio Code in the following snippet:

someDateObject.toLocaleDateString('de-DE', Travel.dateOptions));
someDateObject.toLocaleDateString('de-DE', Travel.dateOptions));

Where Travel.dateOptions is defined as such:

public static dateOptions = { year: 'numeric', month: '2-digit', day: '2-digit' };

This has been working fine for the better part of the last 2 years, but upon opening the class inside VSC recently, it displayed following error for Travel.dateOptions:

Argument of type '{ year: string; month: string; day: string; }' is not assignable to parameter of 
type 'DateTimeFormatOptions'.
Types of property 'year' are incompatible.
Type 'string' is not assignable to type '"numeric" | "2-digit"'. ts(2345)

I am dead confused as to why. Is this possibly a bug with VSC? The code seems to work fine (and has worked fine the entire time) once compiled - and according to the documentation for Date::toLocaleDateString() what I'm doing here seems perfectly valid.

Any ideas?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

When you initialize a class property with a literal such as public foo = { bar: 'a' }, its type becomes { bar: string }, even if you declare it as readonly. TypeScript on purpose doesn't make the type too strict ({ bar: 'a' }).

Method toLocaleDateString accepts an object whose key year must be of type 'numeric' or '2-digit', but yours is of type string.

To make the type of the initialized object more specific, use as const:

public static dateOptions = { year: 'numeric', month: '2-digit', day: '2-digit' } as const;
over 4 years ago · Santiago Trujillo Denunciar

0

I've just hit the same problem, rather than redeclare the const as const (which feels like a code smell) I forced the variable type:

  formatDT(sourceDate: Date) {
    const options: Intl.DateTimeFormatOptions = { month: "long", day: 'numeric', year: 'numeric', hour: '2-digit', minute: "2-digit" };
    return new Intl.DateTimeFormat("en-GB", options).format(new Date(sourceDate));
  }
over 4 years ago · Santiago Trujillo Denunciar

0

Following your code example you must explicitly declare the DateTimeFormatOptions data type for dateOptions

const dateOptions: Intl.DateTimeFormatOptions = {
  year: 'numeric',
  month: '2-digit',
  day: '2-digit'
};

export const Travel = { dateOptions };

const someDateObject = new Date();

console.log(someDateObject.toLocaleDateString('de-DE', Travel.dateOptions));
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda