简体   繁体   English

Typescript:类型“字符串”不可分配给类型“数字”| Date::toLocaleDateString() 中的“2 位”

[英]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:我最近在以下代码段中收到 Visual Studio Code 的“错误”警告:

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

Where Travel.dateOptions is defined as such: Travel.dateOptions 的定义如下:

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 :在过去 2 年的大部分时间里,这一直运行良好,但最近在 VSC 中打开 class 后,它显示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?这可能是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.代码在编译后似乎运行良好(并且一直运行良好) - 根据 Date::toLocaleDateString() 的文档,我在这里所做的似乎完全有效。

Any ideas?有任何想法吗?

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 .当您使用诸如public foo = { bar: 'a' }类的文字初始化 class 属性时,其类型变为{ bar: string } ,即使您将其声明为readonly TypeScript on purpose doesn't make the type too strict ( { bar: 'a' } ). TypeScript 故意不会使类型过于严格( { bar: 'a' } )。

Method toLocaleDateString accepts an object whose key year must be of type 'numeric' or '2-digit' , but yours is of type string .方法toLocaleDateString接受一个 object ,其关键year必须是'numeric''2-digit'类型,但你的类型是string

To make the type of the initialized object more specific, use as const :要使初始化的 object 的类型更具体,请使用as const

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

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:我刚刚遇到了同样的问题,而不是将 const 重新声明为 const (感觉像代码味道),我强制变量类型:

  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));
  }

Following your code example you must explicitly declare the DateTimeFormatOptions data type for dateOptions按照您的代码示例,您必须为dateOptions显式声明DateTimeFormatOptions数据类型

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));

Include "Intl.DateTimeFormatOptions" for the declaration.在声明中包含“Intl.DateTimeFormatOptions”。

const options:Intl.DateTimeFormatOptions = { year: 'numeric', month: '2-digit', day: 'numeric', hour: '2-digit', minute: '2-digit', hour12: false };常量选项:Intl.DateTimeFormatOptions = { 年:'数字',月:'2-digit',日:'数字',小时:'2-digit',分钟:'2-digit',hour12:假 };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM