我有以下代码可以完美运行:
const option = {
year: "numeric",
month: "short",
day: "numeric",
weekday: 'long',
}
const myDate = new Intl.DateTimeFormat('de', {
year: "numeric",
month: "short",
day: "numeric",
weekday: 'long',
}).format(date)
const myDateTest2 = new Intl.DateTimeFormat('de', option as any).format(date)
Run Code Online (Sandbox Code Playgroud)
这意味着如果我将对象直接传递到构造函数中,我会得到一个日期。如果我先定义选项然后使用 ,它也有效any。
但这会引发 TypeScript 错误:
const myDateTest = new Intl.DateTimeFormat('de', option).format(date)
Run Code Online (Sandbox Code Playgroud)
如下:
Argument of type '{ year: string; month: string; day: string; weekday: string; }' is not assignable to parameter of type 'DateTimeFormatOptions'.
Types of property 'weekday' are incompatible.
Type 'string' …Run Code Online (Sandbox Code Playgroud)