I am utilizing an instance of the MUI DatePicker along with a MomentAdapter
:
import *, {useState} as React from 'react';
import TextField from '@mui/material/TextField';
import { AdapterMoment } from '@mui/x-date-pickers/AdapterMoment';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DatePicker } from '@mui/x-date-pickers/DatePicker';
import { Moment } from 'moment';
function App() {
const [value, setValue] = useState<Moment | null>();
return (
<LocalizationProvider dateAdapter={AdapterMoment}>
<DatePicker
value={value}
onChange={(date: Moment | null) => {
setValue(date);
console.log(date);
}}
renderInput={(params) => <TextField {...params} />}
/>
</LocalizationProvider>
)
}
The content of date
appears to be a type of Moment
object, evident through inspection in the console:
https://i.sstatic.net/ZXqE5.png
However, TypeScript is flagging an error as it expects a Date
object instead.
(Interestingly, there are discrepancies when testing this code sourced from MUI documentation on a StackBlitz, where the value does appear to be a date. The issue is compounded by the fact that the StackBlitz console doesn't display objects effectively.)
What should be the appropriate type signature for the function onChange
in this scenario?