如何在中导入 DateTimeFormatOptions

How to import DateTimeFormatOptions in

我想为 date.toLocaleString() 存储一个 DateTimeFormatOptions,以便在我的应用程序的多个位置使用。我这样定义它:

export const timeFormat = { month: 'numeric', day: '2-digit', hour: '2-digit', minute: '2-digit', hour12: false, timeZoneName: 'short', time  Zone: 'UTC'}

我得到:

Argument of type '{ month: string; day: string; hour: string; minute: string; hour12: boolean; timeZoneName: string; timeZone: string; }' is not assignable to parameter of type 'DateTimeFormatOptions'.
  Types of property 'month' are incompatible.
    Type 'string' is not assignable to type '"numeric" | "2-digit" | "short" | "long" | "narrow" | undefined'.

但我不知道导入 DateTimeFormatOptions。最后我只是写了一个格式化日期的辅助方法,但我仍然可能需要导入它,因为我可能允许用户更改日期首选项。

它在 Intl 对象中。您不需要导入它。只需将类型设置为 Intl.DateTimeFormatOptions.

const timeFormat: Intl.DateTimeFormatOptions = { month: 'numeric', day: '2-digit', hour: '2-digit', minute: '2-digit', hour12: false, timeZoneName: 'short', time  Zone: 'UTC'}