Date.prototype.toLocaleDateString() 中的选项类型错误

Jon*_*man 10 typescript

以下代码导致 TypeScript 错误,但工作正常:

components.push(expirationDate.toLocaleDateString(undefined, { dateStyle: 'long' }));

Error:(31, 74) TS2345: Argument of type '{ dateStyle: string; }' is not assignable to parameter of type 'DateTimeFormatOptions'.
  Object literal may only specify known properties, and 'dateStyle' does not exist in type 'DateTimeFormatOptions'.
Run Code Online (Sandbox Code Playgroud)

我确实看到dateStyle确实没有包含在Intl.DateTimeFormatOptions(这是选项对象的类型:

    toLocaleDateString(locales?: string | string[], options?: Intl.DateTimeFormatOptions): string;
    ...
    interface DateTimeFormatOptions {
        localeMatcher?: string;
        weekday?: string;
        era?: string;
        year?: string;
        month?: string;
        day?: string;
        hour?: string;
        minute?: string;
        second?: string;
        timeZoneName?: string;
        formatMatcher?: string;
        hour12?: boolean;
        timeZone?: string;
    }
Run Code Online (Sandbox Code Playgroud)

那么,有没有办法在不简单使用的情况下修复此错误//@ts-ignore

小智 4

Typescript 似乎dateStyle在 v4.2 中添加了这些选项。

以下是介绍它们的 PR: https: //github.com/microsoft/TypeScript/pull/41880

升级到 v4.2.3 修复了我的 Typescript 错误。