小编Ral*_*ndl的帖子

Intl.DateTimeFormat 导致某些选项出现 TypeScript 错误

我有以下代码可以完美运行:

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)

datetime-format typescript

1
推荐指数
1
解决办法
1289
查看次数

标签 统计

datetime-format ×1

typescript ×1