I am using the ant-design date picker component with dayjs as the date library.
The component is controlled with the initial value being null
and the value changes to dayjs object based on some operations in the code.
The below code is a part of the implementation.
function Demo() {
const [now, setNow] = useState<any>(null);
return (
<div>
<p>{JSON.stringify(now)}</p>
<button onClick={() => setNow(dayjs())}>Update Time</button>
</div>
);
}
Currently, I am using any
as the type for now which has to be replaced by a valid type.
I tried the type to be null | typeof dayjs()
But this didn't work.
CodePudding user response:
import { Dayjs } from "dayjs"
Day.js ships with official type declarations for TypeScript in NPM package out of the box.
Dayjs
is the required type of dayjs() object.
Update these two lines in your code.
import dayjs, { Dayjs } from "dayjs";
const [now, setNow] = useState<Dayjs | null>(null);
Check this sandbox for a working code snippet.