Skip to content
← Back to rules

unicorn/consistent-date-clone Style

🛠️ An auto-fix is available for this rule.

它的作用

Date 构造函数在作为参数传入 Date 对象时,可以直接克隆该对象, 因此无需进行时间戳转换。此规则强制使用 直接克隆 Date,而不是使用 .getTime() 进行转换。

为什么这不好?

使用 .getTime()Date 对象转换为时间戳,再转换回 Date, 是多余且不必要的。直接将 Date 对象传给 Date 构造函数会更简洁,也更高效。

示例

以下是此规则的错误代码示例:

js
new Date(date.getTime());

以下是此规则的正确代码示例:

js
new Date(date);

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "unicorn/consistent-date-clone": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/consistent-date-clone": "error",
  },
});
bash
oxlint --deny unicorn/consistent-date-clone

版本

此规则在 v0.15.13 中添加。

参考资料