dayjs对象的typescript类型是什么

6 datepicker typescript reactjs antd dayjs

我使用 ant-design 日期选择器组件和 dayjs 作为日期库。

该组件以初始值为 进行控制null,并根据代码中的一些操作将值更改为 dayjs 对象。

下面的代码是实现的一部分。

function Demo() {
  const [now, setNow] = useState<any>(null);

  return (
    <div>
      <p>{JSON.stringify(now)}</p>
      <button onClick={() => setNow(dayjs())}>Update Time</button>
    </div>
  );
}
Run Code Online (Sandbox Code Playgroud)

目前,我正在使用anyas 类型,必须将其替换为有效类型。

我尝试过的类型是null | typeof dayjs()

但这没有用。

Anj*_*tam 17

从“dayjs”导入 { Dayjs }

Day.js 在 NPM 包中附带了 TypeScript 的官方类型声明,开箱即用。

Dayjs是 dayjs() 对象所需的类型。

更新代码中的这两行。

import dayjs, { Dayjs } from "dayjs";

const [now, setNow] = useState<Dayjs | null>(null);
Run Code Online (Sandbox Code Playgroud)

检查此沙箱中是否有可用的代码片段。